What's wrong with my Firefox and Chrome - in both browsers a simple test produces a result that is contrary to common sense and specifications:

[ true == null, false == null] /* false,false */ 

From the permutation of places the result does not change. Exactly the same picture with undefined :

 [ undefined == true, undefined == false, undefined == null] /* false,false,true */ 

If you create objects of type Boolean , everything becomes as expected:

 [ new Boolean(null) == true, new Boolean(null) == false] /* false,true */ 
  • null is not a value. It can not be compared with the value. And when you create a Boolean(null) it is created with a default value due to the lack of a value. This is FALSE . It is better to check NULL separately. - Saidolim
  • @Saidolim you are a little wrong. Test your theory by calculating Boolean("") and Boolean("1") . - Pavel Mayorov

3 answers 3

Because The Abstract Equality Comparison Algorithm requires if Type(x) or Type(y) are Boolean when comparing x == y , then Boolean should be given a number using ToNumber , which converts true to 1 and false to +0 .

If on simple, then in JS there is no correspondence between bool and null . null in js is type. Therefore, comparing it with something else you always get false .

  • Thanks for the link and the casting explanation. As for null , inaccuracy: null is the only value for the type Null , as they say . - Sergiks
  • w3schools.com/js/js_datatypes.asp Unfortunately, in JavaScript, there is an object - Suvitruf
  • @Suvitruf which means that the data type (the name of which returns typeof ) for null is object . Like, for example, {} . And undefined , for example, the type is undefined . - Regent
  • @Suvitruf You are mistaken. Between null and bool is a match: null is converted to false conditional expressions, such as in an if clause. if (null) matches if (false) - Vlad from Moscow
  • @VladfromMoscow as I said, "comparing null with something else you always get false" - Suvitruf

Because in Javascript, undefined , null , true and false are 4 completely different values.

When converting to boolean, the first two are converted to false , just like 0 and "" - but this does not mean that they are equal to false !

  • How is a loose comparison == in JS? I thought it is reduced to some general type - to some? - Sergiks
  • @Sergiks depends on parameters. There is a complex algorithm (and I do not remember it) - Pavel Mayorov
  • one
    @Sergiks has a nice sign about this: developer.mozilla.org/ru/docs/Web/JavaScript/… - Regent
  • @Regent, inaccurate label, typeof NaN == number but all comparisons with NaN return false - Grundy
  • @Grundy and the comparison of NaN with what, according to the table, should be true ? - Regent

It just needs to be taken into account. This is the so-called "dark" place of the language. It is “dark” because other approaches are no less logical than the accepted one. And the accepted agreement does not look perfect. For example in the if-else clause

 if ( null ) alert( true ); else alert( false ); 

false will be displayed.

Or consider another example.

 var nullValue = Boolean(null); alert("nullValue == false is " + (nullValue == false)); 

The result will be true .

When using the equality operator, the types null and undefined not converted to other types and are equal only to themselves or each other.

Therefore, the only comparisons with the exception of comparisons with themselves where the result of the comparison is true is the following comparisons:

 alert("null == undefined is " + ( null == undefined )); // true alert("undefined == null is " + (undefined == null)); // true 
  • This sentence is the best explanation: “When using the equality operator, the types null and undefined not converted to other types and are equal only to themselves or to each other.” The same, more long and confusing, are explained by the ES5 specs - Sec . 2.3 and 6.7 - Sergiks
  • @Sergiks Yes, if you remember this phrase, then “life will become better, life will become more cheerful.” :) - Vlad from Moscow