JavaScript perversities

Just looking at Douglas Crockford’s “JavaScript: The Good Parts” once again. One of the definitely Not Good parts is the NaN value, supposedly standing for “Not a Number”.

Given that, what would you expect from typeof NaN? Probably not 'number'.

Even better:
NaN === NaN gives false
NaN !== NaN gives true
NaN is not equal to itself!

And best of all, for some reason NaN isn’t a constant, but a global variable. So if you want to change it to something else (e.g. NaN = 'fishcake') you can. Anyone who does should probably be shot, but the fact that it can be done at all is baffling.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.