JavaScript - Array isArray() Method
The JavaScript Array.isArray() method is used to check whether the provided value is an array or not. If it is an array, then this method will return "true", else it returns "false".
Note − This method always returns "false" as result for TypedArray instances.
Syntax
Following is the syntax of JavaScript Array.isArray() method −
array.isArray(object);
Parameters
This method accepts only one parameter. The same is described below −
- The parameter "object" is the object to be checked.
Return value
This method returns a boolean value as reuslt. "True" if the given value is an array, else, false.
Examples
Example 1
In the following example, we are using the JavaScript Array.isArray() method to check whether the variable animals is an array or not.
<html>
<body>
<p id="demo"></p>
<script>
const animals = ["Lion", "Cheetah", "Tiger", "Elephant", "Dinosaur"];
const result = Array.isArray(animals);
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
Output
The output has returned true because animals is an array.
true
Example 2
Here, we are the variable animals is a string, not an array.
<html>
<body>
<p id="demo"></p>
<script>
const animals = "Lion";
const result = Array.isArray(animals);
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
Output
false
Example 3
Following are some other scenarios where the Array.isArray() method will return true −
<html>
<body>
<p id="demo"></p>
<script>
document.write(Array.isArray([]));
document.write(Array.isArray([100]));
document.write(Array.isArray(new Array()));
document.write(Array.isArray(new Array("one", "two", "three", "four")));
document.write(Array.isArray(new Array(619)));
</script>
</body>
</html>
Output
truetruetruetruetrue
Example 4
Following are some other scenarios where the Array.isArray() method will return false −
<html>
<body>
<p id="demo"></p>
<script>
document.write(Array.isArray());
document.write(Array.isArray({}));
document.write(Array.isArray(null));
document.write(Array.isArray(undefined));
document.write(Array.isArray(100));
document.write(Array.isArray("Hello"));
document.write(Array.isArray(true));
document.write(Array.isArray(false));
</script>
</body>
</html>
Output
falsefalsefalsefalsefalsefalsefalsefalse