Why does "undefined equals false" return false?

Javascript

Javascript Problem Overview


When I compare undefined and null against Boolean false, the statement returns false:

undefined == false;
null == false;

It return false. Why?

Javascript Solutions


Solution 1 - Javascript

With the original answer pointing to the spec being deleted, I'd like to provide a link and short excerpt from the spec here.

http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

The ECMA spec doc lists the reason that undefined == false returns false. Although it does not directly say why this is so, the most important part in answering this question lies in this sentence:

The comparison x == y, where x and y are values, produces true or false.

If we look up the definition for null, we find something like this:

NULL or nil means "no value" or "not applicable".

In Javascript, undefined is treated the same way. It is without any value. However, false does have a value. It is telling us that something is not so. Whereas undefined and null are not supposed to be giving any value to us. Likewise, there is nothing it can convert to for its abstract equality comparison therefore the result would always be false. It is also why null == undefined returns true (They are both without any value). It should be noted though that null === undefined returns false because of their different types. (Use typeof(null) and typeof(undefined) in a console to check it out)

What I'm curious of though, is that comparing NaN with anything at all will always return false. Even when comparing it to itself. [NaN == NaN returns false]

Also, another odd piece of information: [typeof NaN returns "number"]


Strict Equality

If possible, you should avoid using the == operator to compare two values. Instead use === to truly see if two values are equal to each other. == gives the illusion that two values really are exactly equal when they may not be by using coercion. Examples:

5 == "5" is true

5 === "5" is false

"" == false is true

"" === false is false

0 == false is true

0 === false is false

Solution 2 - Javascript

So undefined really means undefined. Not False, not True, not 0, not empty string. So when you compare undefined to anything, the result is always false, it is not equal to that.

Solution 3 - Javascript

From the incomparable MDN, sponsored by the company of JavaScript's creator.

> JavaScript provides three different value-comparison operations: > > * strict equality (or "triple equals" or "identity") using ===, > * loose equality ("double equals") using ==, > * and Object.is (new in ECMAScript > 6). > > The choice of which operation to use depends on what sort of > comparison you are looking to perform. > > Briefly, double equals will perform a type conversion when comparing > two things; triple equals > will do the same comparison without type conversion (by simply always > returning false if the types differ); and Object.is will behave the > same way as triple equals, but with special handling for NaN and -0 > and +0 so that the last two are not said to be the same, while > Object.is(NaN, NaN) will be true. (Comparing NaN with NaN > ordinarily—i.e., using either double equals or triple equals—evaluates > to false, because IEEE 754 says so.) Do note that the distinction > between these all have to do with their handling of primitives; none > of them compares whether the parameters are conceptually similar in > structure. For any non-primitive objects x and y which have the same > structure but are distinct objects themselves, all of the above forms > will evaluate to false.

For a visual overview of the whole picture of equality in JavaScript: https://dorey.github.io/JavaScript-Equality-Table/

The truth is, this seemingly "bad" aspect of JavaScript is a source of power when you understand how it works.

Solution 4 - Javascript

Undefined is not the same thing as false, false is a boolean object (which has a value of 0 therefore it is indeed defined).

An example:

var my_var;
var defined = (my_var === undefined)
alert(defined);  //prints true.  It is true that my_var is undefined

my_var = 22;
defined = (my_var === undefined)
alert(defined);  //prints false.  my_var is now defined

defined = (false === undefined)
alert(defined);  //prints false, false is defined

defined = (true === undefined)
alert(defined);  //prints false, true is defined

Solution 5 - Javascript

This is so because it is so. :)

Read the ECMA standards here: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

Solution 6 - Javascript

According to the specification which was mentioned above:

> If Type(y) is Boolean, return the result of the comparison x == > ToNumber(y).

Number(undefined) = NaN;

false == NaN // false

Moreover if we change order false == undefined

> If Type(x) is Boolean, return the result of the comparison ToNumber(x) > == y.

Number(false) = 0;
0 == undefined

There is no rule for this case, so work the default behavior:

> Return false.

Solution 7 - Javascript

You question is half, as we compare undefined/ null to any other types. we will have false return. There is no coercion happening, even we are using == operator.

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
Questionabdul raziqView Question on Stackoverflow
Solution 1 - JavascriptMy Stack OverflowethView Answer on Stackoverflow
Solution 2 - JavascriptasantaballaView Answer on Stackoverflow
Solution 3 - Javascriptjames_womackView Answer on Stackoverflow
Solution 4 - JavascriptTylerView Answer on Stackoverflow
Solution 5 - Javascriptvp_arthView Answer on Stackoverflow
Solution 6 - JavascriptViktor SorokaView Answer on Stackoverflow
Solution 7 - JavascriptNikhil_kView Answer on Stackoverflow