I think "Number" is a better name than "Float". It's a much more common word which I immediately get a feeling for. That typeof NaN === 'number' really makes sense for me, and I feel that most wouldn't need a long explanation to get it either.
I actually do agree that Number is a better name -- particularly for a language that's many people's first experience with programming these days. My comment was a tad sarcastic.
Though if they're not calling them floats, maybe they could have also come up with a better name for NaN. If they used, say, "ERR", it would be something many people had seen before (from calculators) and we'd get to stop hearing people bitch about "NotANumber is a number WTF".
(I suppose "they" in this case must be the hypothetical well-treated team that didn't develop JavaScript instead of a frustrated Brendan Eich.)
Yet another post that underestimates the importance on NaN, it isn't 'WTF' at all if you understand its design. Sure, the naming in ES makes it a bit silly since NaN is a number, but it is something you only have to see once to understand.
Don't mess with IEEE 754, it is almost universally adopted for binary floating-point for very good reasons.
Strings that arent numeric are type coerced into NaN. Using unary plus to coerce to number: +"2" is the primitive number 2 while +"a" is NaN and per the floating point spec, any arithmetic operation on NaN results in NaN.
The correct though unintuitive way to check for NaN is `(x === x) === false`. NaN is a number per the IEEE 754.
The majority of Javascript WTFs are due to type coercion. Use triple equals and check your inputs to avoid.
Yes, it's goofy that typeof (NaN) == 'number'. But it's not that unreasonable to have a numeric value that's explicitly invalid. Pointers and references in other languages have null, which is a legal value for any type of object, but is explicitly invalid if used as an object.
It looks like the blogger is just out and out mistaken. But there's more. The poster goes on about NaN not being easy to check for: not realizing that it's the only value which is never equal to itself. He even thinks this is a bug.
One wonders if this blog post would have been written at all if the author was familiar with NaN in floating-point numbers and knew that isNaN was a thing.
isNaN is the defined method of checking if something Not A Number or can't be converted into a number.
The Real WTF (and infamous "feature" of JS) is that many things can be converted into a number.
The "Harmony" proposal for the next JavaScript spec includes Number.isNaN, which is meant to be a less-confusing version (with no type coercion). It's currently implemented in Firefox Nightly builds: http://bugzil.la/749818
Of all the things to complain about, he chose one that is not JavaScript's fault: the fact that NaN is a number and equality on NaN is not reflexive is a property of the floating point number standard.
Try it in some other language you have lying around, like Python:
In Perl (which doesn't have a concept of a number type distinct from other scalars such as strings, but we can see that it participates in addition differently from non-numeric scalars, which behave like 0):
18 comments
[ 5.0 ms ] story [ 51.3 ms ] threadThough if they're not calling them floats, maybe they could have also come up with a better name for NaN. If they used, say, "ERR", it would be something many people had seen before (from calculators) and we'd get to stop hearing people bitch about "NotANumber is a number WTF".
(I suppose "they" in this case must be the hypothetical well-treated team that didn't develop JavaScript instead of a frustrated Brendan Eich.)
Don't mess with IEEE 754, it is almost universally adopted for binary floating-point for very good reasons.
The correct though unintuitive way to check for NaN is `(x === x) === false`. NaN is a number per the IEEE 754.
The majority of Javascript WTFs are due to type coercion. Use triple equals and check your inputs to avoid.
var x = "a";
isNaN(x); // true
(x === x) === false // false
x = NaN
isNaN(x); // true
(x === x) === false // true
Also, isNaN is global.
You should be harsher on him. NaN is definitely a number in practically every programming language.
It looks like the blogger is just out and out mistaken. But there's more. The poster goes on about NaN not being easy to check for: not realizing that it's the only value which is never equal to itself. He even thinks this is a bug.In this context, 'NaN' just happens to be another admissible value within the space of "number".
isNaN is the defined method of checking if something Not A Number or can't be converted into a number.
The Real WTF (and infamous "feature" of JS) is that many things can be converted into a number.
but you might not expect thatTry it in some other language you have lying around, like Python:
Also Haskell: If anything, it's refreshing that JavaScript does behave like other languages in this case!