How to Check Variable is an Array in JavaScript

Often you need to check if the variable is an array or not. There are many solutions available in javascript but the simplest one is using Array.isArray() method.

Using Array.isArray method in JavaScript

The function Array.isArray() expects one argument which will be the variable and returns true or false depending on the type.
Here is a simple example of using Array.isArray() method in javascript.

The output for the first and second console.log will be false but true for the third. As you can see the subjects is an array type,  with Array.isArray() we have tested it.
Please check mozilla documentation for more information and browser compatibility about Array.isArray() method.

Conclusion

Array.isArray() is a useful method to check if the variable is an array or not. This method will return true or false depending on the variable passed as a parameter.

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here