Parameters
The isArray() method takes a single parameter:
value: The value that you want to check whether it is an array or not. This can be any JavaScript value, such as a variable, an object, a primitive value, or even an array itself.
The isArray() method will evaluate the provided value & determine if it is a true array or not. It does not modify the original value; instead, it simply returns a boolean result indicating whether the value is an array.
Return value
The isArray() method returns a boolean value based on whether the provided value is an array or not. Here are the possible return values:
true: If the provided value is an array, the isArray() method will return true.
false: If the provided value is not an array, the isArray() method will return false.
It's important to note that the isArray() method only checks if the value is a true array, not an array-like object. This means that objects that have a length property & indexed elements, like the arguments object or HTMLCollection, will return false when passed to isArray().
The return value of isArray() can be used in conditional statements, loops, or any other logic that requires determining if a value is an array.
Examples
Example 1: Checking if a variable is an array
const arr = [1, 2, 3, 4, 5];
const num = 42;
console.log(Array.isArray(arr));
console.log(Array.isArray(num));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
false
In this example, we have an array `arr` & a number `num`. We use the isArray() method to check if `arr` is an array, which returns true. We also check if `num` is an array, which returns false since it is a number.
Example 2: Checking if function arguments are an array
function processData(data) {
if (Array.isArray(data)) {
console.log("Data is an array. Processing...");
// Perform array-specific operations
} else {
console.log("Data is not an array. Skipping processing.");
}
}
processData([1, 2, 3]); // Output: Data is an array. Processing...
processData(42);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Data is an array. Processing...
Data is not an array. Skipping processing.
In this example, we have a function `processData` that takes a `data` parameter. Inside the function, we use the isArray() method to check if `data` is an array. If it is an array, we log a message & perform array-specific operations. If it's not an array, we log a different message & skip the processing.
Example 3: Filtering arrays from an array of values
const values = [1, [2, 3], { x: 4 }, [5, 6], "hello", [7, 8, 9]];
const arrays = values.filter(Array.isArray);
console.log(arrays);

You can also try this code with Online Javascript Compiler
Run Code
Outpu
[[2, 3], [5, 6], [7, 8, 9]]
In this example, we have an array of `values` that contains a mix of arrays & other values. We use the `filter()` method along with `Array.isArray()` to create a new array `arrays` that only contains the array elements from `values`. The isArray() method is used as the callback function for `filter()` to determine which elements are arrays.
Supported Browsers
The Array.isArray() method is widely supported across modern web browsers. Here's a list of the browser versions that support this method:
- Chrome: Version 5 & above
- Firefox: Version 4 & above
- Internet Explorer: Version 9 & above
- Opera: Version 10.5 & above
- Safari: Version 5 & above
- Microsoft Edge: All versions
As you can see, the isArray() method has been supported for a long time in all major browsers. This means you can safely use it in your JavaScript code without worrying about compatibility issues.
Frequently Asked Questions
Can the isArray() method distinguish between arrays & array-like objects?
No, the isArray() method only checks if the value is a true array. Array-like objects, such as the arguments object or HTMLCollection, will return false.
Is the isArray() method case-sensitive?
No, the isArray() method is not case-sensitive. You can use Array.isArray() or array.isarray() interchangeably.
Can the isArray() method be used to check if a value is an empty array?
Yes, the isArray() method will return true for an empty array ([]) since it is still an array, regardless of its content.
Conclusion
In this article, we have learned about the JavaScript Array isArray() method. We discussed its syntax, parameters, & return value. We also looked at several examples that showed how to use the isArray() method to check if a value is an array. The isArray() method is widely supported across modern browsers & provides a reliable way to determine if a value is a true array. With the help of this method in your JavaScript code, you can easily perform array-specific operations & handle arrays effectively.
You can also check out our other blogs on Code360.