Emmanuel Akinfulubi

JavaScriptBasic

Check if a value is an array in JavaScript

Beginner · 4 min read

Code often receives a value and needs to know what it is before it can use it. An API might send back one item or a list of items. A function might accept either a single name or an array of names. Before you call .map or .length, you need to be sure you have an array.

In this tutorial you will see why the obvious tool, typeof, can't answer that question. Then you will use Array.isArray, the reliable answer, and write a small helper, kindOf, that names the exact kind of almost any value. You need to know what a function and an array are.

What typeof does

typeof is an operator that tells you the basic type of a value, as a string:

JavaScript

console.log(typeof 42);        // number
console.log(typeof 'text');    // string
console.log(typeof true);      // boolean
console.log(typeof undefined); // undefined

For these simple values it works well. The trouble starts with objects.

Arrays are objects

In JavaScript, an array is a special kind of object. So is a date, and so is a regular expression. typeof only knows the broad category, so it gives the same answer for all of them:

JavaScript

console.log(typeof []);         // object
console.log(typeof {});         // object
console.log(typeof new Date()); // object
console.log(typeof /re/);       // object
console.log(typeof null);       // object

That last line is a famous mistake. null means "no value", and it isn't an object at all. The 'object' answer is a bug from JavaScript's first version, in 1995. It can never be fixed, because too much existing code on the web depends on it.

Here is the exercise. Try it yourself first if you like, or follow the steps.

34 Check whether a value is an array

easy

Decide reliably whether a value is an array. Show why typeof is no help.

Stuck? Three hints, smallest first
  1. 1. typeof [] is "object", same as for a plain object and for null.
  2. 2. Array.isArray() is the correct answer and has been since ES5.
  3. 3. Object.prototype.toString.call(v) is the older trick and still useful for distinguishing other built-ins.

Step 1: look at the problem

Run typeof over a mix of values and see how little it tells you:

JavaScript

const values = [[], {}, 'text', 42, null, undefined, new Date(), /re/];
for (const v of values) console.log(typeof v);
// object, object, string, number, object, undefined, object, object

Five different things all come out as 'object'. You can't tell the array from the others.

Step 2: use Array.isArray

Array.isArray(value) was added to the language for exactly this job. It returns true for arrays and false for everything else:

Step 2

const values = [[], {}, 'text', 42, null, undefined, new Date(), /re/];
for (const v of values) {
  console.log(String(typeof v).padEnd(10), Array.isArray(v) ? 'array' : 'not an array');
}

Only the first line says array. A plain object, a string, null and a date are all correctly rejected. padEnd(10) adds spaces to the end of the text until it is 10 characters long, so the two columns line up.

A string is worth a second look. It has a length and you can read 'abc'[0], so it feels a bit like an array. Array.isArray('abc') is still false, which is what you want.

Step 3: name the exact kind

Sometimes you need more than "array or not". Is it a date? A regular expression? null? There is an older trick for that. Object.prototype.toString is a built-in method that every object inherits, and when you call it on a value it reports the value's built-in kind:

JavaScript

console.log(Object.prototype.toString.call([]));         // [object Array]
console.log(Object.prototype.toString.call(new Date())); // [object Date]
console.log(Object.prototype.toString.call(null));       // [object Null]

.call(value) runs the method with value as the thing it reports on. The answer always looks like [object Something]. The word we want starts at position 8 (after [object and a space) and ends one character before the end (before the ]).

slice(8, -1) cuts exactly that part. A negative position counts from the end, so -1 means "stop before the last character". Then toLowerCase() makes it easy to compare:

Step 3

const kindOf = (v) => Object.prototype.toString.call(v).slice(8, -1).toLowerCase();
console.log(kindOf([]));         // array
console.log(kindOf({}));         // object
console.log(kindOf(null));       // null
console.log(kindOf(new Date())); // date
console.log(kindOf(/re/));       // regexp
console.log(kindOf(42));         // number

Unlike typeof, kindOf gives null its own name and tells arrays, dates and regular expressions apart.

When to use which

  • To ask "is this an array?", use Array.isArray. It is short, clear and always right.
  • To ask "is this a string or a number?", typeof is fine.
  • When you need the exact built-in kind of an object, keep kindOf handy.

The full solution

The complete answer runs typeof and Array.isArray side by side over eight different values, then prints the kindOf name for each one. Press Run to see the output.

The answer

const values = [[], {}, 'text', 42, null, undefined, new Date(), /re/];

// typeof cannot tell an array from an object, or from null.
for (const v of values) {
  console.log(String(typeof v).padEnd(10), Array.isArray(v) ? 'array' : 'not an array');
}

// The general-purpose version, for when you need the exact built-in.
const kindOf = (v) => Object.prototype.toString.call(v).slice(8, -1).toLowerCase();

console.log('');
for (const v of values) console.log(kindOf(v));

Output


      
10 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 10 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

10 checks

test('typeof cannot distinguish an array', typeof [], 'object');
test('nor null', typeof null, 'object');
test('isArray gets it right', Array.isArray([]), true);
test('and rejects an object', Array.isArray({}), false);
test('and rejects a string', Array.isArray('abc'), false);
test('and rejects null', Array.isArray(null), false);
test('kindOf names an array', kindOf([]), 'array');
test('kindOf names a date', kindOf(new Date()), 'date');
test('kindOf names null', kindOf(null), 'null');
test('kindOf names a regexp', kindOf(/re/), 'regexp');

typeof null === 'object' is a bug from 1995 that can never be fixed without breaking the web, and typeof [] being 'object' follows from arrays genuinely being objects. Array.isArray exists precisely because neither is useful. kindOf is worth keeping in your toolkit for the cases isArray does not cover.

Common mistakes

  • Checking typeof value === 'object' to find arrays. Plain objects, dates and null pass too.
  • Checking for a length property. Strings have one, and so can ordinary objects. Array.isArray asks the real question.
  • Forgetting that typeof null is 'object'. Code like typeof v === 'object' && v.name crashes on null. Check v !== null as well.

Try it yourself

  1. Write toArray(value) that returns value unchanged if it is an array, and [value] otherwise.
  2. Use kindOf on new Map(), new Set() and a function. What does it say for each?
  3. Write isPlainObject(v) that is true for {} but false for arrays, dates and null.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.