Reputation: 859
When I compare undefined and null against Boolean false, the statement returns false:
undefined == false;
null == false;
It return false. Why?
Upvotes: 54
Views: 57121
Reputation: 14982
In JavaScript, the loose equality operator (==) follows specific “abstract equality” rules for comparing values of different types. When you compare undefined to false using undefined == false, those rules do the following:
The left-hand side is undefined.
The right-hand side is a Boolean (false).
To compare a Boolean to something else under ==, JavaScript first converts the Boolean to a number: false → 0.
Now you’re effectively comparing undefined to 0. When comparing undefined to a number, the result is always false (because undefined is not coerced to any numeric value).
Hence, undefined == false yields false.
By contrast, note that undefined only loosely equals null (undefined == null → true) according to JavaScript’s equality rules. But it does not equal false, 0, an empty string, or anything else.
Read the ECMA standards here: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3
Upvotes: -2
Reputation: 1
undefined
and false
are distinct values. false
is a defined Boolean
value, while undefined
signifies the absence of a value.
Upvotes: 0
Reputation: 4934
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 why 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"]
If possible, you should avoid using the ==
(two equals) operator to compare two values. Instead use ===
(three equals) 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 equal 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
Upvotes: 47
Reputation: 217
According to the specification flow:
If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
1) undefined == false; -> undefined == Number(false); -> undefined == 0;
2) null == false; -> null == Number(false); -> null == 0;
There is no rule for these cases, so go with the behavior in the last step:
Return false.
Upvotes: -2
Reputation: 59
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.
Upvotes: -1
Reputation: 10296
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.
Upvotes: 1
Reputation: 18157
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
Upvotes: -2
Reputation: 4038
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.
Upvotes: 0