How to useArray.isArray() Method in Javascript
The Array.isArray() method determines whether the value passed to this function is an array or not. This method returns true if the argument passed is an array else it returns false.
Syntax:
Array.isArray( obj )
Parameter:
- obj is any valid object in JavaScript like map, list, array, string, etc.
Return Value: It returns Boolean value true if the object passed is an array or false if the object passed is not an array.
Example 1: This example uses Array.isArray() method to check the object is array or not.
Javascript
function checkObject() { const countries = [ "India" , "USA" , "Canada" ]; const checkArrayObj = Array.isArray(countries); console.log(checkArrayObj); } checkObject(); |
Output
true
Example 2: This example uses Array.isArray() function to check the object is array or not.
Javascript
function checkObject() { // It returns false as the object // passed is String not an array console.log( Array.isArray( 'hello w3wiki' ) ); } checkObject(); |
Output
false
How to check object is an array in JavaScript ?
In this article, we will see how to check whether the given object is an array or not in JavaScript. There are two approaches, these are:
- Using Array.isArray() Method
- Using typeof Operator
Contact Us