How to Check if an Object is an Array in JavaScript: 3 Best Ways

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Check if an Object is an Array in JavaScript: A Complete Guide

\n\n

Have you ever caught yourself thinking: "I thought I passed an array, but the code crashes with an error"? A familiar situation. In JavaScript, arrays are objects, and a simple typeof check won't work here. Today we'll break down three main ways to check if an object is an array, and figure out which one to use in real projects.

\n\n

Why typeof Doesn't Work?

\n\n

Before diving into the details, let's understand why beginners often stumble here. In JavaScript, arrays are a special case of objects. Therefore, the typeof operator for an array will return "object", not "array". Try it yourself:

\n\n
const arr = [1, 2, 3];\nconsole.log(typeof arr); // "object" — useless
\n\n

Moreover, typeof null === "object" is a well-known bug in the language that won't be fixed. So if you're checking data from a user or an API, you can't rely on typeof. More reliable methods are needed.

\n\n

Method 1: Array.isArray() — The Modern Standard

\n\n

Since ES5 (2009), JavaScript has had a built-in method Array.isArray(). This is the most correct and simple way to check if an object is an array.

\n\n
const arr = [10, 20, 30];\nconst notArr = { length: 3, 0: 'a', 1: 'b', 2: 'c' };\n\nconsole.log(Array.isArray(arr));    // true\nconsole.log(Array.isArray(notArr)); // false\nconsole.log(Array.isArray('hello')); // false\nconsole.log(Array.isArray(null));    // false
\n\n

Why is this the best choice? Because Array.isArray() works correctly even in multi-context scenarios: if an array comes from another iframe or frame, instanceof can break, but Array.isArray() won't. It checks the internal [[Class]] type of the object, not the prototype chain.

\n\n

Browser Support

\n

The method is supported by all modern browsers, including Internet Explorer 9+. If you need to support very old browsers (IE 8 and below), use a polyfill or the third method.

\n\n

Method 2: instanceof Array — The Classic Approach

\n\n

The instanceof operator checks whether the Array.prototype prototype exists in the object's prototype chain. It sounds complicated, but in practice it works like this:

\n\n
const arr = [1, 2, 3];\nconsole.log(arr instanceof Array); // true\n\nconst obj = { a: 1 };\nconsole.log(obj instanceof Array); // false\n\n// But here's the surprise:\nconst iframeArr = new iframe.contentWindow.Array(1, 2, 3);\nconsole.log(iframeArr instanceof Array); // false! (if the array is from another frame)
\n\n

Here's the catch: if you have multiple iframes on a page or you're working with Web Workers, arrays from different global contexts have different prototypes. instanceof doesn't account for this and can return false for a real array. Therefore, in modern development, it's better to avoid instanceof for checking arrays.

\n\n

Method 3: Object.prototype.toString.call() — The Universal Soldier

\n\n

This method was used before Array.isArray() appeared. It is still relevant if you need to write a polyfill or work in an environment where Array.isArray is not available.

\n\n
function isArray(obj) {\n  return Object.prototype.toString.call(obj) === '[object Array]';\n}\n\nconsole.log(isArray([1, 2]));      // true\nconsole.log(isArray({ a: 1 }));    // false\nconsole.log(isArray('string'));    // false\nconsole.log(isArray(null));        // false
\n\n

How does it work? The toString method of the Object.prototype object returns a string like [object Type]. For arrays, this is always [object Array]. This method bypasses the iframe problem because it checks the internal

Blogs

Book Recommendations