But… And there’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.
typeof null // "object"
typeof null outputs
null instead of
According to MDN docs, this is done for legacy reasons.
- type tag
And there were 5 types of tags. The tag
0 stood for
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 // "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
NaN was to be treated as a specific value, calculations would become problematic. For example, what would
NaN / NaN result in?
You are probably familiar with the
isNaN function to check for these types. But are you aware of the two different
isNaN('dan') // true Number.isNaN('dan') // false
isNaN()global scope function
- will return true if the value is a NaN, or will be co-erced to a NaN
- will return true only if the value is already a NaN
The first one returns
'dan' will be coerced into a
NaN. Second-line returns
'dan' is not a
NaN value as-is.
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' // runs, no error thrown
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 email@example.com if you know what’s up.
That’s all. Happy programming!