Open your console and type:
typeof null
You'll get:
'object'
That's wrongβnull is not an object!
π How did this happen?
When JavaScript was first implemented, values were stored in a tagged format, where the lower bits determined the type:
-
Objects
had a type tag of 0. -
null
was stored as 0
in memory. Since
null
had 0
in its type tag, typeof
mistakenly classified it as an "object". π§ Why isn't it fixed?
One word: Backward compatibility.
Fixing it would break too many apps, so it's here to stay!
π¬ Share this with JavaScript haters
new
#JavaScript #Bug #Programming