26

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.

5
  • 2
    yup ...can you point me to some material where it says what the reason is and not just that it is equal :P Commented May 17, 2013 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. Commented May 17, 2013 at 11:25
  • 1
    can you please explain how the == operator actually operates when it comes down to comparing undefined and null Commented May 17, 2013 at 11:28
  • possible duplicate of null vs. undefined and their behaviour in JavaScript or undefined and null Commented May 17, 2013 at 11:29
  • for a full explanation and the complete set of rules check this: webreflection.blogspot.dk/2010/10/… Commented Sep 17, 2013 at 8:58

6 Answers 6

41

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).

Sign up to request clarification or add additional context in comments.

9 Comments

Err, yes. It's part of the core language.
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 ???
"when we use == the types are made equal" - no (at least, not always), see the link on the first line of my answer.
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 :(
The specification link is pure gold, thanks, stop arguing about how you like javascript and go and read the docs instead.
|
13

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.

3 Comments

This should be the best answer. Great explanation!
In Javascript "==" applies type coercion. Both "null" and "undefined" are "falsey" primitives. Let's hope this helps drive the logic home!
This may sound dumb but when null == undefined, which of the two is type coerced into the other?
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.

3 Comments

can you please explain what is being converted to what ?
Because thats what the language spec says should happen. There doesn't need to be a reason other than "the documentation says so"
"0" (String zero) is not false, the rules is a non empty string is always true.
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

Comments

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);

Comments

-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

6 Comments

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.
Yet another down-vote... Please explain why if you down-vote a post. I'm trying to help here, not trying to lose reputation.
It is still wrong. It doesn't check false equals false, which bit is hard to understand. Read Quentin's answer.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.