25

If we alert(null==undefined) it outputs to true.

What is the logical reason for this.

Is this something that is hard coded in javascript or is there an explanation for this.

steo
  • 4,586
  • 2
  • 33
  • 64
Nav
  • 10,304
  • 20
  • 56
  • 83
  • 2
    yup ...can you point me to some material where it says what the reason is and not just that it is equal :P – Nav May 17 '13 at 11:24
  • I'd say because undefined means nothing aswel, If your div doesn't have an ID, it's undefined, or we could say, non existant, null, void. – Stefan Candan May 17 '13 at 11:25
  • 1
    can you please explain how the == operator actually operates when it comes down to comparing undefined and null – Nav May 17 '13 at 11:28
  • possible duplicate of [null vs. undefined and their behaviour in JavaScript](http://stackoverflow.com/questions/7000762/null-vs-undefined-and-their-behaviour-in-javascript) or [undefined and null](http://stackoverflow.com/questions/6031372/undefined-and-null) – Bergi May 17 '13 at 11:29
  • for a full explanation and the complete set of rules check this: http://webreflection.blogspot.dk/2010/10/javascript-coercion-demystified.html – Maurizio In denmark Sep 17 '13 at 08:58

6 Answers6

40

The language specification explicitly says:

If x is null and y is undefined, return true

I'm not aware of any records of the language design process that explain the reasoning for that decision, but == has rules for handling different types, and "null" and "undefined" are both things that mean "nothing", so having them be equal makes intuitive sense.

(If you don't want type fiddling, use === instead).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    Err, yes. It's part of the core language. – Quentin May 17 '13 at 11:57
  • tell me one thing...when we use == the types are made equal and then checked for equality right? the type of undefined is undefined and type of null is object then is object==undefined ??? – Nav May 17 '13 at 12:00
  • "when we use == the types are made equal" - no (at least, not always), see the link on the first line of my answer. – Quentin May 17 '13 at 12:01
  • can you please check Spudley 's answer and explain me.... i have become confused as to how type coercion is done for undefined and null :( – Nav May 17 '13 at 12:05
  • @Nav — It isn't. There are specific rules for what to do when comparing undefined to null and they kick in before the rules for converting to different types. – Quentin May 17 '13 at 12:06
  • This kinda things sometimes makes me think that javascript is all about remembering a random trivia of badly written code and its repercussions :( – Nav May 17 '13 at 12:07
  • I don't think I've ever come across a programming language where a loosely typed comparison operator made complete sense. In general, I recommend sticking to `===` unless you know you want type coercion (which will usually be only when you are comparing a String or Number to a variable that you expect to contain a String or Number). – Quentin May 17 '13 at 12:09
  • thanks man ..yeah i do myself use === but one interviewer asked me this question...so i thought i will clear things up :) – Nav May 17 '13 at 12:12
  • 2
    The specification link is pure gold, thanks, stop arguing about how you like javascript and go and read the docs instead. – Agustín Amenabar Dec 04 '13 at 01:01
12

Using the double-equal operator forces Javascript to do type coercion.

In other words, when you do x == y, if x and y are not of the same type, JavaScript will cast one value to another before comparing, like if string and number are compared, the string is always cast into a number and then compared

For this reason, many comparisons of mixed types in JavaScript can result in results that may be unexpected or counter-intuitive.

If you want to do comparisons in JavaScript, it is usually a better idea to use the triple-equal operator === rather than double-equal. This does not do a type coercion; instead if the types are different, it returns false. This is more usually what you need.

You should only use double-equal if you are absolutely certain that you need it.

Spudley
  • 166,037
  • 39
  • 233
  • 307
6

For the same reason that 0 == "0" - javascript is loosely typed - if something can be converted to something else then it will be unless you use ===

alert(null===undefined);

Will give you false.

As for why these particular conversions happen - the answer is quite simply "the spec says that is what should happen". There doesn't need to be a reason other than "because it says so" for why programming language behave in certain ways.

Edit: Slightly better answer - in Javascript, certain objects/values are 'truthy' or 'falsey' when converted to a boolean. 0 (integer zero), "0" (character zero in a string), "" (empty string) are all false. If there isn't a better comparison to use then the boolean operation applies.

This is why "0" is not equal to an empty string, but both "0" and "" are both equal to false.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
0

we know,

If x is null and y is undefined, return true

undefined == null => true, reason might be both are converted to boolean, as we know javascript performs the type conversion. so that will be resulting the null and undefined converted to false and false == false is true

Mukesh Bhati
  • 504
  • 4
  • 5
0

A better explanation...

In the case of "==" or Loose Equality Operator, if one of the operands is null or undefined and the other is null or undefined, always return true. Otherwise return false. Unlike what other posters falsely state, these are not converted or coerced into new types when using equality operators, but simply follow the rule above.

        // TRUE - loose equality operator says use the null vs. undefined rule above which equates them
        console.log(null == undefined);

        // FALSE - strict equality operator says these are not of the same type, so always return false
        console.log(null === undefined);
Stokely
  • 12,444
  • 2
  • 35
  • 23
-2

The == comparison operator doesn't check the types. null and undefined both return false. That's why your code is actually checking if false is equal to false.

> null == undefined;
< true
> false == false
< true

However their types are not equal.

> typeof undefined;
< "undefined"
> typeof null;
< "object"

Because of that, the next statement will return false, as the === comparison operator checks both the types and their value.

> undefined === null;
< false
Broxzier
  • 2,909
  • 17
  • 36
  • 1
    The answer, `it just makes booleans, numbers or strings of the data that needs to be checked` is incorrect as explained by Quentin's answer. `null` and `undefined` aren't converted to another type, they have there own rules. – Ash Burlaczenko May 17 '13 at 11:47
  • Yet another down-vote... Please explain why if you down-vote a post. I'm trying to help here, not trying to lose reputation. – Broxzier May 17 '13 at 11:55
  • 1
    It is still wrong. It doesn't check `false` equals `false`, which bit is hard to understand. Read Quentin's answer. – Ash Burlaczenko May 17 '13 at 11:57
  • It does not state `false == false` is the same as `null == undefined`. It just shows that `false == false` also returns `true`. For example `!!undefined` and `!!null` both return `false`. – Broxzier May 17 '13 at 12:01
  • Umm, your answer *does* state that. I can't see any other interpretation of *That's why your code is actually checking if false is equal to false*. – Quentin May 17 '13 at 12:04
  • @AshBurlaczenko Just to correct you, `==` and `===` they both check the types of the values, but the only difference is that double equal do type coercion and triple equal don't. – Haitham6373 Aug 31 '21 at 14:02