Dan D Kim

Let's share stories

Javascript is weird

2020-06-15 Dan D. Kimjavascript

Javascript is a wonderful language. I’m very happy to be working with Javascript at my full-time job.

But… And ther’s always a “but”.

There are things that make me confused.

And it’s not “practical” to address it.

So, I just shove it under the rug.

Until today.

typeof null

typeof null // "object"



null is a primitive in Javascript, and thus it would make more sense that the typeof null outputs null instead of object.

According to MDN docs, this is done for legacy reasons.

Let’s go back to the days when we didn’t have Google Maps and had to print out directions on paper. In those days, Javascript values were represented by two things:

  1. type tag
  2. value

And there were 5 types tags. The tag 0 stood for object.

The thing is, null, or the NULL pointer, was represented as 0x00 on most platforms. So null got a type tag of 0. And as tradition has it, we still need to respect this today.

typeof NaN

typeof NaN // "number"



NaN is technically a numeric data type. It’s just a data type that is supposed to replace values that cannot be represented with actual numbers. NaN doesn’t mean it’s not a numeric data type. It just means the data can’t be represented with numbers.

But it doesn’t mean it’s a specific value either. See what I mean:

NaN === NaN // false

If NaN was to be treated as a specific value, calculations would become problematic. For example, what would NaN / NaN result in?

Detecting NaN

You are probably familiar with the isNaN function to check for these types. But are you aware of the two different isNaN functions?

isNaN('dan') // true
Number.isNaN('dan') // false

Confused Gandalf


  • isNaN() global scope function

    • will return true if the value is a NaN, or will be co-erced to a NaN
  • Number.isNaN()

    • will return true only if the value is already a NaN

First one returns true because 'dan' will be co-erced into a NaN. Second line returns false because 'dan' is not a NaN value as-is.


emoji-warning Certain array methods cannot detect NaN, as the following example from MDN docs shows.

let arr = [2, 4, NaN, 12];
arr.indexOf(NaN);                      // -1 (false)
arr.includes(NaN);                     // true
arr.findIndex(n => Number.isNaN(n));   // 2


undefined = 'defined'

Confused Obama

I ain’t got an explanation for this one. Might be just a bug?

I would normally expect a SyntaxError, like I would get if I do null = 1.

Lemme know at dougouk@gmail.com if you know what’s up.

That’s all. Happy programming!