Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 256
iatboy Avatar asked Dec 24 '14 06:12

iatboy


People also ask

Why null == null is false?

Answer : There no relative aspect between null and boolean. 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.

Why null === null is true?

2. How to check for null. missingObject === null evaluates to true because missingObject variable contains a null value. If the variable contains a non-null value, like an object, the expression existingObject === null evaluates to false .

Does null return true or false?

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.

Why null == undefined is true?

Both undefined and null are falsy by default. So == returns true. But when we use the strict equality operator (===) which checks both type and value, since undefined and null are of different types (from the typeof Operator section), the strict equality operator returns false.


2 Answers

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.

like image 155
Sean Vieira Avatar answered Sep 21 '22 23:09

Sean Vieira


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

like image 29
Gayan C Liyanage Avatar answered Sep 24 '22 23:09

Gayan C Liyanage