1

I'm reading a book "Eloquent JavaScript" by Marijn Haverbeke and it says:

"The rules for converting strings and numbers to Boolean values state that 0, NaN, and the empty string ( "" ) count as false, while all the other values count as true."

Would be very nice if someone explains me what did the author mean by saying that NaN counts as false according to the rules of converting?

As I can see it now:

0 == false;     // true
"" == false;    // true
NaN == false;   // false
0 / 0 == false; // false

Yes I know that "NaN is not equal to anything, even to the other NaN", but I just wonder what does the book want me to know?

Mike
  • 1,979
  • 2
  • 16
  • 29

4 Answers4

3

Basically, if a variable is assigned to NaN, it will evaluate to false if used in a conditional statement. For example:

var b = NaN;

if(b){//code will never enter this block
    console.log('the impossible has occurred!')
}

This is true as well if you get invalid input, for example:

var input = "abcd"
var num = parseFloat(input);

if(num){
    console.log('the impossible has occurred!')
}
Will P.
  • 8,437
  • 3
  • 36
  • 45
1

var a = NaN;
var b = null;
var c;
var d = false;
var e = 0;

document.write(a + " " + b + " " + c + " " + d + " "+ e + "<br>");
if(a || b || c || d || e){
  document.write("Code won't enter here");
}
if(a || b || c || d || e || true){
  document.write("Code finally enters");
}

Reference: link

Community
  • 1
  • 1
Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27
1

Other answers are correct, but the significant thing here is that a conversion to a bool takes place in the case that it's used in a condition.

That's why:

NaN === false // false

Yet:

if(NaN) // false because it first does a boolean conversion
        // which is the rule you see described

As a side note, NaN == false as used in the question (note == vs ===) actually does a type conversion from false to 0 per the == operator. It's beyond the scope of this question, but the difference in operators is well documented elsewhere.

aw04
  • 10,857
  • 10
  • 56
  • 89
  • How is it possible to compare NaN == false without converting it to a bool then? – Mike May 02 '17 at 20:44
  • 1
    It doesn't convert it in that case, that's why NaN == false is false, the conversion is when you use it as a condition (which makes sense if you think about it, because it must be a bool) – aw04 May 02 '17 at 20:46
  • 1
    Here's a cool table btw if you're interested in more rules -> http://dorey.github.io/JavaScript-Equality-Table/ – aw04 May 02 '17 at 20:53
  • 2
    @Mike: When you use `==` to compare against a boolean value, the boolean is actually converted to a number. So `NaN == false` is really `NaN == 0`. *Converting* to a boolean is very different from *comparing* with a boolean. – Felix Kling May 02 '17 at 20:58
  • @FelixKling This makes sense, thank you for the information! – Mike May 02 '17 at 21:01
  • @FelixKling Thanks you're right, updated the answer for clarity – aw04 May 02 '17 at 21:06
0

The book wants you to know that JavaScript evaluates NaN to false.

var notANumber = 500 / 0;
if(notANumber) {
     // this code block does not run
}
else {
        // this code block runs
}
victor
  • 802
  • 7
  • 12