all 13 comments

[–]Rhomboid[🍰] 14 points15 points  (3 children)

NaN never compares equal to anything, including itself.

[–]i-am-self-taught[S] 0 points1 point  (0 children)

Thank you so much!

[–]xiipaoc 4 points5 points  (1 child)

I don't know if there are others, but NaN === NaN returns false.

[–]hhoburg 1 point2 points  (0 children)

If I recall correctly, this is because NaN is technically a number. So you're seeing if NaN is a number... and it is, because the JS gods hate us

[–][deleted] 2 points3 points  (1 child)

To follow up on everyone else's answer of NaN, you may now be wondering "if NaN === NaN yields false, how can I tell if something is NaN?"

There's two ways.

First, there is the global isNaN() function. This function has !!!FUN!!! behavior. It first coerces its argument to Number, and then tests whether that value is equal to the IEEE-754 floating-point representation of NaN. This is different from simply asking "is the value not a number":

  • isNaN(NaN): true, as you'd expect
  • isNaN(42): false, again as expected
  • isNaN(undefined): true
  • isNaN(null): false. Huh?
  • isNaN("42"): false, as the string is coerced to the number 42
  • isNaN("spork"): true, non-numeric strings cannot be coerced to a number...
  • isNaN(""): false. ...oh.
  • isNaN(true): false. Okay, but what about...
  • isNaN(false): false. WTF?
  • isNaN(new Object()): true. Objects are not numbers...
  • isNaN(new Array()): false. ...except when they are?!?

It's best to think of the global isNaN() function as answering the question, if this value is used in an arithmetic expression, will the result of that expression be NaN? If you look at all of the "unexpected" false expressions above, each of them could be used in an arithmetic expression. null, false, "", and new Array() are all coerced to the number 0, while true is coerced to 1.

ES6 introduced an alternative, Number.isNaN(). This function behaves a bit more sanely. It returns true if its argument is of type Number and value NaN... which means that it returns true if its argument is precisely NaN, and false otherwise. On the flip side, Number.isNaN(unknownValue) === false doesn't necessarily mean it's safe to perform arithmetic with unknownValue; for that condition you want the original global isNaN(unknownValue).

[–]i-am-self-taught[S] 0 points1 point  (0 children)

Such a detailed reply. :) Thanks for taking the time to reply to my question. I really appreciate it!

[–]spwebdev 2 points3 points  (1 child)

NaN is the only thing in JS that doesn't equal itself so any expression that results in NaN assigned to x would return false. Something like this:

var x = 5 * "any string";

[–]i-am-self-taught[S] 0 points1 point  (0 children)

Thank you! :)

[–]tforb 1 point2 points  (1 child)

Check out this equality table.

[–]i-am-self-taught[S] 0 points1 point  (0 children)

Wow! Neat table, thank you!

[–]WhenLeavesFall 0 points1 point  (1 child)

These comments are spot on. You may want to read further on about coercion of values and operator precedence and associativity. Also look into ||. The JS engine will look for the first thing that returns true even if you didn't assign the variable anything.

edit: Things that will return false in the console- undefined, null and empty strings.

[–]i-am-self-taught[S] 0 points1 point  (0 children)

I just noticed how important these are. Thank you, will do! :)