The Array.isArray() method in JavaScript is used to determine if a given value is an array. This method is particularly useful for type-checking, ensuring that the value you are working with is indeed an array before performing array-specific operations.
Syntax
Array.isArray( value );
Parameters
Parameter | Description |
---|---|
value | The value to be checked if it is an array. |
Return Value
Return Type | Description |
---|---|
Boolean | Returns true if the value is an array, otherwise returns false. |
Example 1: Checking if a Value is an Array
This code checks if the given value arr is an array. If it is an array, it prints true; otherwise, it prints false.
let arr = [1, 2, 3]; // Output: true console.log(Array.isArray(arr));
Example 2: Checking a String
This code checks if the given value str is an array. Since it is a string, it prints false.
let str = "Hello, World!"; console.log(Array.isArray(str)); // Output: false
Example 3: Checking an Object
This code checks if the given value obj is an array. Since it is an object, it prints false.
let obj = { name: "John", age: 30 }; console.log(Array.isArray(obj)); // Output: false
Example 4: Checking undefined
This code checks if the given value undef is an array. Since it is undefined, it prints false.
let undef; console.log(Array.isArray(undef)); // Output: false
Example 5: Checking null
This code checks if the given value n
is an array. Since it is null
, it prints false
.
let n = null; console.log(Array.isArray(n)); // Output: false
Example 6: Checking an Array-Like Object
This code checks if the given value arrayLike
is an array. Since it is an array-like object, but not an actual array, it prints false
.
let arrayLike = { 0: "a", 1: "b", length: 2 }; console.log(Array.isArray(arrayLike)); // Output: false
Supported Browsers
Chrome | Edge | Firefox | Opera | Safari |
---|---|---|---|---|
4 | 12 | 4 | 10.5 | 5 |