yburyug
yburyug

Reputation: 1070

JavaScript Weirdness - typeof [] === typeof {} evaluates to 'true'

I know the strict equals in JS evaluates two things: equality and like-typedness

Object.is() is the closest comparison I could find to gather further insight and it offered little further in my investigation.

Can anyone who groks the innards of JS better explain this? Is an array a very very strange implementation of an object? So they are empty and evaluate the same here?

I'm quite perplexed and just curious.

Upvotes: 4

Views: 379

Answers (2)

Elliot Bonneville
Elliot Bonneville

Reputation: 53301

Arrays are objects with a number of additional methods -- MDN defines them as "high-level, list-like objects," and goes on to say:

Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations.

Upvotes: 2

user1726343
user1726343

Reputation:

typeof returns the primitive type of the operand. For anything other than bools, numbers, strings, functions, and undefined, this is object.

Upvotes: 3

Related Questions