49

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

undefined == false;
null == false;

It return false. Why?

Hanky Panky
  • 44,997
  • 8
  • 67
  • 93
abdul raziq
  • 799
  • 1
  • 5
  • 10
  • 15
    Why would you expect it to be true? – SLaks Oct 09 '13 at 16:23
  • Because `false` is a valid boolean value. Whereas `undefined` and `null` are not. So they're not equal. – David Oct 09 '13 at 16:23
  • i mean undefined is also false so false == false return true. – abdul raziq Oct 09 '13 at 16:23
  • 8
    @SLaks Because `0 == false` is true, etc. – Wesley Murch Oct 09 '13 at 16:24
  • 6
    @abdulraziq: `"i mean undefined is also false"` - No it's not. It's `undefined`. – David Oct 09 '13 at 16:24
  • 3
    BTW, you should use strict equality. – SLaks Oct 09 '13 at 16:25
  • @WesleyMurch ok removing my comment. – Mr_Green Oct 09 '13 at 16:27
  • @Mr_Green that confused me even more – abdul raziq Oct 09 '13 at 16:28
  • 1
    @abdulraziq forget it.. think it as nightmare.. :) – Mr_Green Oct 09 '13 at 16:28
  • 9
    To illustrate the difference between `false` and `undefined`... I have 3 apples. I give 3 apples to Jane. Do I have any apples left? (false) Do I have any oranges left? (undefined) – David Oct 09 '13 at 16:29
  • If you think about it, in practical terms, `undefined` and `false` aren't equivalent. Say you have an object with a property which will be set to `true` or `false`, but only once an external network call has been made to confirm which value is appropriate. In this case, if this property is undefined, that means the network call hasn't returned yet; it doesn't mean that the property's value can be considered to be `false`. – Paul D. Waite Oct 09 '13 at 16:30
  • See http://www.mapbender.org/JavaScript_pitfalls:_null,_false,_undefined,_NaN for an explanation of all this Javascript magic. – JBeagle Oct 09 '13 at 16:32
  • I like this article: http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/ – vp_arth Oct 09 '13 at 16:38
  • in if condition if i put undefined its works as expected so why its not working in comparison ? – abdul raziq Oct 09 '13 at 16:45
  • @WesleyMurch: Sure `0 == false`, but `0 != null` and `0 != undefined` either? – Bergi Aug 06 '15 at 17:11
  • Undefined and false aren't equivalent but JS is truthy/falsey making that irrelevant. If I create a new, empty object `var obj = {}`, it appears contradictory that `obj.fakeProp == false` is `false` while `!obj.fakeProp` is `true`. I would expect `obj.fakeProp == false` to evaluate to true and `obj.fakeProp === false` to evaluate to false. – xr280xr Sep 25 '18 at 18:06

7 Answers7

40

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

My Stack Overfloweth
  • 3,994
  • 3
  • 22
  • 34
0

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.

asantaballa
  • 3,549
  • 1
  • 17
  • 20
  • 12
    `undefined == null` is true. – SLaks Oct 09 '13 at 16:24
  • 1
    Wow, @SLaks. Appears so, and I was surprised, and kind of disappointed. Read up and appears that it is because the "==" operator can do coercion between the two. Agree with Xander that the "===" Does not do and to me is more"intuitive". Glad you brought up, learned something myself today! – asantaballa Oct 09 '13 at 16:30
  • @asantaballa null == undefined because it is defined to be so: http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 – Soul Ec Oct 09 '13 at 16:31
  • 2
    Always use === when possible in JavaScript. == gives the illusion that two values really are exactly equal when they may not be. (5 == "5" is true. 5 === "5" is false.) – My Stack Overfloweth Oct 09 '13 at 16:33
0

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.

Community
  • 1
  • 1
james_womack
  • 9,456
  • 6
  • 50
  • 72
  • No, MDN isn't created by a company. Those articles are written by arbitrary volunteers, it's a wiki :-) – Bergi Aug 06 '15 at 17:09
  • 1
    @Bergi I'm well aware of who contributes to MDN. MDN itself is created by Mozilla though. If you're going to be pedantic, get it right. – james_womack Aug 06 '15 at 19:04
-1

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

vp_arth
  • 12,796
  • 4
  • 33
  • 59
  • If you are going to link to a standard, link to the official one: http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 – Soul Ec Oct 09 '13 at 16:44
  • 2
    Find you any difference before downvoting? – vp_arth Oct 09 '13 at 16:45
  • Other than the fact that your link is older by two years (yours is from 2009, mine is from 2011), what does your link and response add that my link/explanation (posted 14 minutes before this) omitted? – Soul Ec Oct 09 '13 at 16:47
  • @SoulEc I don't see it while search through my bookmarks, sorry. Is this a real problem? what should I do? – vp_arth Oct 09 '13 at 16:50
  • if you aren't adding anything to the discussion, it's better to just remove your answer. – Soul Ec Oct 09 '13 at 16:51
  • 2
    Oh, really? Why? is it wrong? Is it less concise? Your answer is right too, I upvote it and wish you it be accepted – vp_arth Oct 09 '13 at 17:04
  • 2
    I've experienced frustrations in the past where the first result for a google search is an SO answer was a link-only answer for an expired link or a "read the spec" answer. Worst experience ever. I don't know who is running ecma262-5.com (seems like a guy just paying for hosting), but I do know that ecma-international.org is actually run by an organization with funds – Soul Ec Oct 09 '13 at 17:10
-1

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.

  • 1
    `ToNumber` is never called on `undefined`. Not sure where you got that from. – Bergi Aug 06 '15 at 17:16
  • It is from specification, so I just got it from here. If you have something specific to say, go ahead. – Viktor Soroka Aug 06 '15 at 19:00
  • I say that you misread or misunderstood the specification. Btw, it would be nice to link that document you are citing. – Bergi Aug 06 '15 at 19:02
-1

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.

Nikhil_k
  • 31
  • 2
-2

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
Tyler
  • 15,509
  • 9
  • 45
  • 81