Earlier today, on Twitter, I posted a couple of fun JavaScript quirks that really make no sense at all. There are a plethora of little oddities in the language, like this one: did you know that NaN does not equal itself, or anything for that matter? Better yet, try finding “typeof NaN.” It ironically returns “number,” despite all logic.
typeof NaN === ‘number’ // true
One of these “quirks/errors” received a couple responses, implying that I was incorrect. Now I’m learning this language just as much as anyone, so it’s possible, maybe even likely, that I’m wrong. But am I?
The discussion involved the fact that “typeof null” incorrectly returns “object” - or at least incorrect as I’ve learned. Nevertheless, a few people responded to me with the information that this is not a mistake, and is actually common among many programming languages. As they put it, null “is supposed to be an object.”
What strikes me as odd is that every piece of text I’ve read on the subject speaks to the contrary. As I understand things, this was simply an error that dates back to the first release of the JavaScript engine, which was then carried over to Microsoft’s JScript. The reason it hasn’t been removed is due to the fact that it could potentially break numerous programs at this point.
Unfortunately, this backward compatibility issue is the reasoning behind numerous errors and inconsistencies in languages. For example, ever wonder why so many browsers incorrectly label themselves, within the user-agent string, as being Mozilla? The answer goes back a long way…