Why (null == false) and (null == true) both return false?

JavascriptNullBoolean

Javascript Problem Overview


I know that null is an object with no attributes or functions.

However, I am confused that why console.log(null == false); and console.log(null == true); both return false.

What are the conversion rules between null and boolean?

Javascript Solutions


Solution 1 - Javascript

This is because the Abstract Equality Comparison Algorithm requires that if Type(x) or Type(y) is a Boolean in the expression x == y then the Boolean value should be coerced to a number via ToNumber, which converts true to 1 and false to +0.

This means that any comparison of true == something or something == true results in 1 == something or something == 1 (replacing true and 1 with false and +0 for false).

The Null type does not compare as equal to either 1 or +0 (in fact, null is only comparable to undefined in the Abstract Equality Comparison Algorithm).

There is a detailed discussion of all of the different kinds of equality in JavaScript on MDN that is well worth looking at if you want to know more.

However, if you coerce null to a number it is coerced to +0 so +null == false actually returns true.

Solution 2 - Javascript

Adding to the current discussion. null >= false returns true.

I believe that it is because this is interpreted as !(null < false)

Solution 3 - Javascript

Answer : There no relative aspect between null and boolean.

MDN Source:-

> The value null is a literal (not a property of the global object like > undefined can be). In APIs, null is often retrieved in place where an > object can be expected but no object is relevant. When checking for > null or undefined beware of the differences between equality (==) and > identity (===) operators (type-conversion is performed with the > former). > > // foo does not exist, it is not defined and has never been initialized: > > foo > "ReferenceError: foo is not defined" >
> // foo is known to exist now but it has no type or value: > > var foo = null; foo > "null" > > Difference between null and undefined > > typeof null // object (bug in ECMAScript, should be null) > typeof undefined // undefined > null === undefined // false > null == undefined // true

JavaScript | MDN

Solution 4 - Javascript

The value null is a JavaScript literal represents an "empty" value or "undefined". null is one of JavaScript's primitive values. It is neither equal to boolean true nor equal to boolean false because it's value is undefined. The value of null is more inclined towards false even though it is not false. That's why it's called "falsey" operators and an if (var) { } block does not get executed when var is null.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniatboyView Question on Stackoverflow
Solution 1 - JavascriptSean VieiraView Answer on Stackoverflow
Solution 2 - JavascriptVictor Hernandez de CrespoView Answer on Stackoverflow
Solution 3 - JavascriptGayan C LiyanageView Answer on Stackoverflow
Solution 4 - JavascriptDiptenduView Answer on Stackoverflow