Skip to main content

How to Check if a Value Is an Array in JavaScript with Array.isArray

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Array.isArray(value) gives true when the value is an array and false for anything else, including strings, null and objects that only look like arrays. It's the check to use, because typeof says 'object' for an array.

Syntax​

Syntax
Array.isArray(<value>)
PartWhat it does
Array.isArrayA static method of Array: you call it on Array itself, not on the value.
<value>Any value. The method never throws, whatever you pass.

It returns true or false. MDN's Array.isArray reference: "The Array.isArray() static method determines whether the passed value is an Array."

The Example​

The example checks an array of two colours, an empty array, the string 'red', an object with a 0 key and a length, and null. The last line shows what typeof says about the same array.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which logs Array.isArray for an array of red and green, an empty array, the string red, an object with key 0 and length 1, and null, then logs typeof of the array.

main.js
console.log(Array.isArray(['red', 'green']));
console.log(Array.isArray([]));
console.log(Array.isArray('red'));
console.log(Array.isArray({ 0: 'red', length: 1 }));
console.log(Array.isArray(null));
console.log(typeof ['red', 'green']);

Step 2: Run It​

Terminal
node main.js
Output
true
true
false
false
false
object

A zsh terminal where node main.js prints true, true, false, false, false and object.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • Both arrays gave true, the empty one included.
  • The string 'red' gave false. A string has a length and indexes, but it isn't an array.
  • The object { 0: 'red', length: 1 } gave false. It looks like an array from the outside, but it wasn't made as one.
  • null gave false without an error, so you don't need a null check first.
  • typeof gave object for the array, which is why it can't tell an array from any other object.

Common Mistakes​

MistakeWhat happensDo this instead
Checking with typeof value === 'object'Objects and null pass as wellUse Array.isArray(value)
Calling it on the value: value.isArray()TypeError, because arrays have no isArray methodCall it on Array: Array.isArray(value)
Treating a typed array, such as Uint8Array, as an arrayArray.isArray gives false for itCheck for the typed array's own class with instanceof

Troubleshooting​

SymptomLikely causeFix
TypeError: roles.join is not a functionThe value is a string or an object, not an arrayCheck Array.isArray(roles) before using array methods
An array check passes for a string in another check you wroteThe check used length or indexesUse Array.isArray

FAQ​

What's the difference between Array.isArray and instanceof Array?​

For arrays made in your own code, both give true. MDN says Array.isArray() "is preferred over instanceof because it works across realms": an array made in another realm, such as another frame in a browser, gives false with instanceof Array.

Is an array made with new Array(3) an array?​

Yes. Array.isArray(new Array(3)) is true, even though its three slots are empty.

Conclusion​

Array.isArray is the reliable way to know you have an array before you call array methods on it. It returns false for everything else, null included, and it never throws.

Additional Resources​