Last Updated : 15 Jul, 2024
The isArray()
method in JavaScript is used to determine whether a given value is an array or not. This method returns true if the argument passed is an array else it returns false.
Array.isArray(obj);Parameters:
This function returns the Boolean value true if the argument passed is an array otherwise it returns false.
Example 1: Checking if a Value is an Array using Array.isArray()
The code defines a function func()
checking if 'foobar'
is an array using Array.isArray()
. It logs false
. 'foobar'
isn't an array, confirming the method's accuracy.
// JavaScript code for isArray() function
function func() {
console.log(Array.isArray('foobar'));
}
func();
Example 2: Passing map in isArray() Method
Since the argument passed to the function isArray() is a map therefore this function returns false as the answer.
JavaScript
// JavaScript code for isArray() function
function func() {
console.log(Array.isArray({ foo: 123 }));
}
func();
Example 3: Illustration of Array.isArray() Method on nested Array.
JavaScript
// Array.isArray() method on 1D Array
const arr=[1,2,3,4,5,7,6];
const result=Array.isArray(arr)
console.log(result);
//Array.isArray() method on nested Array
const num = [1, [4, 9, 16], 25];
const sqRoot = num.map(num => {
if (Array.isArray(num))
{
//to iterate over the nested array
return num.map(innerNum => Math.sqrt(innerNum));
} else
{
return Math.sqrt(num);
}
});
console.log(sqRoot); // Output: [1, [2, 3, 4], 5]
true [ 1, [ 2, 3, 4 ], 5 ]
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
Supported Browsers:RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4