JavaScript Array isArray() Syntax & Examples isArray() static-method The Array.isArray() method in JavaScript returns true if the argument is an array, or false otherwise.
Syntax of isArray() The syntax of Array.isArray() static-method is:
Array.isArray(value)
This isArray() static-method of Array returns true if the argument is an array, or false otherwise.
Parameters Parameter Optional/Required Description value
required The value to be checked.
Return Type Array.isArray() returns value of type Boolean
.
✐ Examples 1 Using Array.isArray() method to check if a value is an array In JavaScript, we can use the Array.isArray() method to check if a value is an array.
For example,
We define a variable arr with an array value [1, 2, 3]. We use the Array.isArray() method with arr to check if it is an array. The result is stored in the variable isArray. We log isArray to the console using console.log() method to see if arr is an array. JavaScript Program const arr = [1, 2, 3];
const isArray = Array.isArray(arr);
console.log(isArray);
Copy 2 Using Array.isArray() method to check if a value is not an array We can use the Array.isArray() method to check if a value is not an array.
For example,
We define a variable notArray with a non-array value 'hello'. We use the Array.isArray() method with notArray to check if it is not an array. The result is stored in the variable isArray. We log isArray to the console using console.log() method to see if notArray is an array. JavaScript Program const notArray = 'hello';
const isArray = Array.isArray(notArray);
console.log(isArray);
Copy 3 Using Array.isArray() method to check if an object is an array We can use the Array.isArray() method to check if an object is an array.
For example,
We define a variable obj with an object value { key: 'value' }. We use the Array.isArray() method with obj to check if it is an array. The result is stored in the variable isArray. We log isArray to the console using console.log() method to see if obj is an array. JavaScript Program const obj = { key: 'value' };
const isArray = Array.isArray(obj);
console.log(isArray);
Copy Summary In this JavaScript tutorial, we learned about isArray() static-method of Array: the syntax and few working examples with output and detailed explanation for each example.