This question has already been answered:

I was faced with such a situation that an empty line is not a true or a false

'' === true // false '' === false // false 

Is there any explanation for this?

Reported as a duplicate by Darth , Grundy javascript 26 Jun '18 at 12:48

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • 3
    because it is a string and not a boolean variable? - teran
  • @teran I understand that this is not a boolean variable, but an array, for example, is also not a boolean, but in the test it is true if ([]) console.log(123) // 123 and if ('') console.log(123) // ничего - Roma Boyko
  • Oddly enough, because an empty line is neither a true nor a fals - Grundy
  • an array, for example, is also not boolean, but in checking it is true if ([]) and in question on your if - Grundy

1 answer 1

You use the comparison operator === , which also takes into account the data type of the operands. And since on the one hand you have a string variable, and on the other a boolean, the result will be negative. As for example, for 1 === true or 0 === false .

With the usual == comparison, you will get a different result due to the implicit type conversion.

 console.log( '' === false) ; console.log( '' == false); 

In the usual comparison, false itself is calculated as false , 0 , "", '' , null , undefined , NaN . Everything else is true , like the one you mentioned.

Again, when comparing === equality of data types matters. This is the whole essence of this operator.