Table of contents
1.
Introduction
2.
Syntax
3.
Parameters
4.
Return value
5.
Examples
5.1.
Example 1: Checking if a variable is an array
5.2.
Example 2: Checking if function arguments are an array
5.3.
Example 3: Filtering arrays from an array of values
6.
Supported Browsers
7.
Frequently Asked Questions
7.1.
Can the isArray() method distinguish between arrays & array-like objects?
7.2.
Is the isArray() method case-sensitive?
7.3.
Can the isArray() method be used to check if a value is an empty array?
8.
Conclusion
Last Updated: Oct 13, 2024
Easy

JavaScript Array isArray() Method

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaScript is a famous, user-friendly but most importantly powerful programming language used for web development. Just like other programming languages, JavaScript is also incomplete without arrays. Arrays are like a list in programming, where you can store several items together under one name. To make operations easy on Arrays, javascript offers many built-in methods & functions. This makes it easier to organize and manage your data. One such method is the isArray() method. This method helps determine whether a given value is an array or not. 

JavaScript Array isArray() Method

In this article, we will discuss the JavaScript Array isArray() method in detail, like its syntax, parameters, return value, & examples. 

Syntax

The syntax for the JavaScript Array isArray() method is : 

Array.isArray(value)


In this syntax, "value" represents the value you want to check whether it is an array or not. The isArray() method is called directly on the Array object, & it takes a single argument.

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.

Live masterclass