why null==undefined is true in javascript

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.

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

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.

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 ===


Will give you false.

Read More:   angular ui modal after close event

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.

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

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

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

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts