Last modified: Aug 03, 2026
JavaScript Array isArray() Guide
JavaScript developers often need to know if a value is an array. The Array.isArray() method is the reliable way to do this. It returns true if the given value is an array, and false otherwise.
This guide explains everything you need to know. You will learn the syntax, see examples, and understand why this method is better than other checks. Let's dive in.
Why Use Array.isArray()?
Checking for arrays in JavaScript can be tricky. The typeof operator returns "object" for arrays. This is not helpful because many things are objects. Using typeof alone cannot tell you if a value is an array or a plain object.
The Array.isArray() method solves this problem. It is a static method on the Array constructor. This means you call it directly on Array, not on an array instance. It is simple, fast, and works across different JavaScript environments.
This method is especially useful when working with data from APIs or user input. You can safely check the data type before performing array operations.
Syntax and Parameters
The syntax is very straightforward. You pass one argument to the method.
Array.isArray(value)
The value parameter is the item you want to test. It can be any JavaScript value. This includes strings, numbers, objects, functions, or even null and undefined.
The return value is always a boolean. It is true if the value is an array. It is false for everything else.
Remember, you do not create a new array to use this method. You just call it directly on the global Array object.
Basic Examples
Let's look at some basic examples to see how it works. We will test different data types.
// Test different values
console.log(Array.isArray([1, 2, 3])); // true
console.log(Array.isArray([])); // true (empty array)
console.log(Array.isArray('hello')); // false (string)
console.log(Array.isArray(42)); // false (number)
console.log(Array.isArray({name: 'Sam'})); // false (object)
console.log(Array.isArray(null)); // false
console.log(Array.isArray(undefined)); // false
true
true
false
false
false
false
false
As you can see, the method correctly identifies arrays. It returns false for all other data types. This is exactly what we need.
Notice that an empty array [] returns true. This is correct because it is still an array, just with no items.
Real-World Use Cases
The Array.isArray() method is very practical. Here are some common scenarios where you will use it.
First, you might receive data from a web API. You cannot always trust the data format. You can use this method to verify that you got an array before looping through it.
Second, you might write a function that accepts different types of arguments. You can use this check to handle arrays differently from other values.
Third, when working with JSON data, arrays and objects look similar. This method helps you distinguish between them.
Let's see a practical example with a function.
function processData(data) {
// Check if data is an array
if (Array.isArray(data)) {
// It's an array, so we can use array methods
return data.map(item => item * 2);
} else {
// It's not an array, handle it differently
return 'Data is not an array';
}
}
console.log(processData([1, 2, 3]));
console.log(processData('hello'));
[ 2, 4, 6 ]
Data is not an array
This function checks the input type first. It then applies the correct logic. This prevents errors and makes your code more robust.
Array.isArray() vs instanceof
Another common way to check for arrays is using instanceof. However, Array.isArray() is the recommended approach. Why? Because instanceof can fail in certain situations.
The main issue is with multiple JavaScript contexts. This can happen in web browsers with iframes or windows. An array created in one iframe is not an instance of the Array constructor in another iframe.
Let's demonstrate this problem.
// Simulate an array from another context
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
const iframeArray = iframe.contentWindow.Array;
// Create an array using the iframe's constructor
const arr = new iframeArray(1, 2, 3);
// Check with instanceof
console.log(arr instanceof Array); // false in some browsers
// Check with Array.isArray()
console.log(Array.isArray(arr)); // true
false
true
As you can see, instanceof returns false even though the value is an array. The Array.isArray() method works correctly. It checks the internal class of the value, not the constructor.
So, always use Array.isArray() for reliable results. It is the standard and safest way to check for arrays.
Edge Cases and Special Values
Let's explore some edge cases. The method handles all JavaScript values gracefully. It never throws an error.
What about arguments objects? These are array-like but not true arrays. The method returns false for them.
function testArguments() {
console.log(Array.isArray(arguments));
}
testArguments(1, 2, 3); // false
false
What about typed arrays like Uint8Array? They are also not regular arrays. The method returns false for them.
const typedArray = new Uint8Array([1, 2, 3]);
console.log(Array.isArray(typedArray)); // false
false
The method also returns false for promises, functions, and symbols. It only returns true for actual array objects.
This strictness is a feature. It ensures you are working with the exact data type you expect.
Using isArray() with Other Array Methods
You can combine Array.isArray() with other array methods. This is a common pattern in real code. It helps you write safer and more flexible functions.
For example, you might want to flatten an array only if it is an array. You can check first, then use methods like flat() or reduce().
Let's see an example with reduce(). This is a powerful method, and you can learn more in our guide on JavaScript Array reduce() Explained.
function sumArray(input) {
// Check if it's an array
if (!Array.isArray(input)) {
return 'Not an array';
}
// Use reduce to sum all elements
return input.reduce((sum, num) => sum + num, 0);
}
console.log(sumArray([1, 2, 3, 4]));
console.log(sumArray('error'));
10
Not an array
This pattern is very useful. You can also use it with methods like map() or filter(). Always validate your data first.
For more advanced array operations, you might want to explore how to Group JavaScript Array Items by Key. This also relies on checking array types correctly.
Performance and Best Practices
The Array.isArray() method is very fast. It is a native method, so it is optimized by the JavaScript engine. You can call it many times without worrying about performance.
Here are some best practices to follow. Always use this method instead of instanceof. It is more reliable and works in all contexts.
Use it early in your functions. This helps you fail fast and avoid errors later. It also makes your code easier to read and debug.
Do not use typeof to check for arrays. It is not accurate. Combine Array.isArray() with other checks if needed.
Remember that arrays are objects. So, typeof [] returns "object". This is why you need this special method.
Conclusion
The Array.isArray() method is an essential tool in JavaScript. It provides a simple and reliable way to check if a value is an array. This helps you write safer code that handles different data types correctly.
We covered the syntax, basic usage, and edge cases. You learned why it is better than instanceof. You also saw practical examples of how to use it in your projects.
Remember to always validate your data. Use Array.isArray() before performing array operations. This simple step can prevent many bugs and errors.
Now you can confidently work with arrays in JavaScript. For more array tips, check out our guide on JavaScript Array Methods Guide. You can also learn about other useful methods like JavaScript Array of Arrays Guide to deepen your knowledge.