Using Array isArray() method you can test whether the value passed is an Array or not in JavaScript. This method returns true
if an object is an array, otherwise false
.
Array.isArray(obj)
The isArray()
method, being a static method, is called using the Array
class name.
JavaScript Array isArray()
Simple example code if finds the passed value is an array, it returns True. Otherwise, it returns False.
<!DOCTYPE html>
<html>
<body>
<script>
value1 = "JavaScript";
console.log(Array.isArray(value1));
value2 = 18;
console.log(Array.isArray(value2));
value3 = [];
console.log(Array.isArray(value3));
value4 = [1, 2, 3, 4];
console.log(Array.isArray(value4));
value5 = new Array(3);
console.log(Array.isArray(value5));
value6 = new Uint8Array(16);
console.log(Array.isArray(value6));
</script>
</body>
</html>
Output:
Here are a few examples:
Array.isArray([]); // true
Array.isArray([1, 2, 3]); // true
Array.isArray(new Array()); // true
Array.isArray({}); // false
Array.isArray("Hello"); // false
Array.isArray(123); // false
In the first three examples, isArray()
returns true
because the values being checked are arrays. In the last three examples, isArray()
returns false
because the values are not arrays.
It’s important to note that the isArray()
method was introduced in ECMAScript 5 (ES5), so it is supported by all modern browsers and JavaScript environments.
Comment if you have any doubts or suggestions on this JS Array method.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version