coding
array
object

How can I check if an object is an array?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In JavaScript, the correct answer is usually short: use Array.isArray(value). The reason this question keeps coming up is that arrays are objects, typeof returns "object", and some older checks fail when values come from another realm such as an iframe. If you know why those older checks fail, the rule becomes simple and easy to defend.

Use Array.isArray

Array.isArray is the standard, readable, cross-realm-safe way to test for arrays.

javascript
1console.log(Array.isArray([]));
2console.log(Array.isArray([1, 2, 3]));
3console.log(Array.isArray({ length: 2 }));
4console.log(Array.isArray("abc"));
5console.log(Array.isArray(null));

This prints true, true, false, false, and false.

The important point is that Array.isArray checks the actual array brand, not whether something merely looks array-like.

Why typeof Does Not Work

A common beginner mistake is to write this:

javascript
console.log(typeof []);

That prints "object", because arrays are objects in JavaScript. The typeof operator is useful for primitives and functions, but it does not distinguish plain objects from arrays.

So if your goal is "is this value specifically an array," typeof does not answer the question you are asking.

Why instanceof Array Can Be Misleading

This also works in many local cases:

javascript
console.log([] instanceof Array);

But instanceof depends on the constructor identity in the current realm. If an array was created in another window or iframe, it may not share the same Array constructor object.

That means instanceof Array can return false even though the value is genuinely an array.

In modern code, instanceof is best treated as a narrower local check, not as the primary answer.

Object.prototype.toString as a Fallback

Before Array.isArray was widely available, developers often used Object.prototype.toString.call(value).

javascript
1function isArrayFallback(value) {
2  return Object.prototype.toString.call(value) === "[object Array]";
3}
4
5console.log(isArrayFallback([]));
6console.log(isArrayFallback({}));

This still works, and it handles cross-realm values more reliably than instanceof. But it is more verbose and less direct than Array.isArray, so it is mainly useful for understanding older code or very old JavaScript environments.

Arrays Versus Array-Like Values

Another source of confusion is the difference between arrays and array-like values. A value can have numeric keys and a length property without being an actual array.

Examples include:

  • the arguments object in older function patterns
  • many DOM collections
  • custom objects such as {"0": "a", "1": "b", length: 2}
javascript
const arrayLike = { 0: "a", 1: "b", length: 2 };
console.log(Array.isArray(arrayLike));
console.log(Array.from(arrayLike));

Array.isArray(arrayLike) is false, which is correct. If you want a real array, convert it explicitly with Array.from rather than pretending it already is one.

Typed Arrays Are Different Too

Typed arrays such as Uint8Array are not normal arrays.

javascript
const bytes = new Uint8Array([1, 2, 3]);
console.log(Array.isArray(bytes));
console.log(ArrayBuffer.isView(bytes));

The first line prints false. That is not a bug. Typed arrays are separate built-in types designed for binary data.

If your code should accept both ordinary arrays and typed arrays, write that rule explicitly instead of assuming one check covers both.

Common Pitfalls

The most common mistake is using typeof and expecting arrays to report a unique type name. They do not.

Another mistake is using instanceof Array in code that may receive values from another realm. That often works in tests and then fails in browser integrations.

Developers also sometimes confuse "iterable" or "array-like" with "array." Those are related ideas, but they are not equivalent. Choose the check that matches the contract your function actually requires.

Finally, do not overengineer this. If you are writing modern JavaScript and need to know whether a value is an array, Array.isArray is already the right answer.

Summary

  • Use Array.isArray(value) for normal JavaScript array checks.
  • Do not use typeof to distinguish arrays from objects.
  • 'instanceof Array can fail across realms such as iframes.'
  • 'Object.prototype.toString.call(value) is a valid older fallback.'
  • Array-like objects and typed arrays are not the same thing as ordinary arrays.

Course illustration
Course illustration

All Rights Reserved.