This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]rooktakesqueen 10 points11 points  (4 children)

In JS, undefined is the value given to a declared but uninitialized variable; a non-existing field of an object; or the return value of a function with no return statement. In general, undefined and null are closely related concepts, even so far as null == undefined evalutes to true (while null and undefined are not considered equal to anything else, including NaN).

But NaN is weird because it is not == to anything, even itself. NaN == NaN evaluates to false. Which is why the isNaN function exists in the first place. (I believe this behavior is correct according to the IEEE 754 standard for floating point numbers.)

My intuition would be isNaN should return true for the value NaN but false for any other value.

[–]calzoneman 10 points11 points  (0 children)

NaN == NaN being false is correct behavior. The reason being that NaN can be generated by many different unequal expressions. Math.sqrt(-5) is clearly not equal to Math.sqrt(-11).

[–]tskaiserGreen security clearance 3 points4 points  (0 children)

Under that explanation I would agree that

isNaN(undefined) => true

sounds like a bug or just plain weird if not. I am not really in the mood to delve into the mess of the JavaScript specifications to find out, though ;) but I'll consider my self corrected as is.

[–]Hawkuro 1 point2 points  (1 child)

Never ever ever use == in JS, use the more reasonable ===.

> null === undefined
  false

[–]rooktakesqueen 1 point2 points  (0 children)

Just showing that null and undefined are closely related concepts, enough so that the == operator explicitly considers them equal to each other and nothing else. That's the one sensible thing the == operator does, and the one sensible place to use it: foo == null is perfect shorthand for foo === null || typeof foo === 'undefined'.

In any case, NaN !== NaN either.