DEV Community

Cover image for Why typeof( [ ] ) Returns "object" in JavaScript
Kathirvel S
Kathirvel S

Posted on

Why typeof( [ ] ) Returns "object" in JavaScript

If you’ve been writing JavaScript for even a short time, you’ve probably seen this:

typeof []
// "object"
Enter fullscreen mode Exit fullscreen mode

And your brain immediately goes:

Wait… what? That’s clearly an array. Why is JavaScript lying to me?

Let’s break this down properly — not just the surface-level explanation, but the real reason this happens.


First: JavaScript Has Only a Few Real Types

In JavaScript,typeof only returns one of these values:


"undefined"
"boolean"
"number"
"string"
"bigint"
"symbol"
"function"
"object"
Enter fullscreen mode Exit fullscreen mode

Notice anything missing?

There’s no "array".

That’s not an accident.


Arrays Are Objects. Period.

In JavaScript, arrays are not a separate primitive type.

When you write:

const arr = [];
Enter fullscreen mode Exit fullscreen mode

You’re actually doing:


const arr = new Array();
Enter fullscreen mode Exit fullscreen mode

And Array is a constructor that creates an object.

So technically:


typeof []
// "object"
Enter fullscreen mode Exit fullscreen mode

is completely correct.


What Makes Arrays Special Then?

If arrays are just objects, why do they behave differently?

Because arrays are specialized objects with:

Numeric keys ("0", "1", "2", ...)

A dynamic length property

Built-in methods like map, filter, reduce

For example:

const arr = ["a", "b"];

console.log(Object.keys(arr));
// ["0", "1"]

console.log(arr.length);
// 2
Enter fullscreen mode Exit fullscreen mode

Internally, it behaves very close to:

{
  "0": "a",
  "1": "b",
  length: 2
}
Enter fullscreen mode Exit fullscreen mode

But with special internal behavior.

In the ECMAScript spec, arrays are called “exotic objects” — meaning they’re objects with special internal logic.


Why typeof Doesn’t Return "array"

Now the real question.

Why didn’t JavaScript just make typeof [] return "array"?

Two reasons:

1 .Historical design

JavaScript was created in 1995 in just 10 days by Brendan Eich.

The language was intentionally simple:

  • A few primitives
  • Everything else as objects

Arrays were implemented as objects from day one.

2 . Backward compatibility

JavaScript has one golden rule:

Don’t break the web.

Iftypeof [] suddenly started returning "array", it would break millions of websites relying on "object".

So it stays.


The Right Way to Check for Arrays

Since typeof isn’t enough, we use:

Array.isArray([])
Enter fullscreen mode Exit fullscreen mode

This returns:

true
Enter fullscreen mode Exit fullscreen mode

And this is the recommended and safest way.

Avoid doing this:

typeof arr === "array" // ❌ doesn't work
Enter fullscreen mode Exit fullscreen mode

Bonus: The Even Stranger Case of null

If this surprised you, try this:

typeof null
// "object"
Enter fullscreen mode Exit fullscreen mode

That’s actually a historical bug from early JavaScript that was never fixed for compatibility reasons.

Yes. JavaScript has some scars from the 90s 😅


Prototype Chain Perspective (The Real Mental Model)

Here’s the clean mental model:


[] instanceof Array   // true
[] instanceof Object  // true
Enter fullscreen mode Exit fullscreen mode

Why?

Because:

Array → Object
Enter fullscreen mode Exit fullscreen mode

Arrays inherit from Array.prototype, which inherits from Object.prototype.

So arrays are objects with extra powers.


The Real Takeaway

Instead of thinking:

"Why is JavaScript wrong?"

Think:

"Arrays are objects with special behavior."

Once you internalize that, a lot of JavaScript quirks start making sense.


Quick Summary

typeof [] returns"object"

Arrays are specialized objects

typeof has a limited return set

Use Array.isArray() to detect arrays

JavaScript prioritizes backward compatibility over perfection

Top comments (0)