Vijay Barnwal
Vijay Barnwal

Reputation: 154

Why does null == undefined evaluate to true?

I have a code below which is not clear to me

var a = null; 
if(a==undefined)
alert("true");
else 
alert("false");

When I ran above code it alerts true.

Can anybody explain whats the reason or concept behind this?

Upvotes: 7

Views: 3756

Answers (7)

Godfred
Godfred

Reputation: 1

Comparison operators operate by converting operands into numbers before comparing. I understood this by an example, which is: Consider you're writing a program to give access to employees into their account. You ask for credentials and the user input nothing. Ideally, you keep asking until it's provided because null is equal only to undefined under ==, which a special case in comparison operation.

Upvotes: 0

onefootswill
onefootswill

Reputation: 4107

The spec - Clause 11.9.3 . See clauses 2 and 3.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1075527

It's true because == is the loose equality operator, and null and undefined are loosely equal (null == undefined is true). If you use the strict equality operator, ===, they are not equal (null === undefined is false).

Basically, the loose equality operator will coerce its operands if they're of different types (see Abtract Equality Comparison in the spec). 0 == "" is true, for instance, because if you coerce "" to a number, it's 0. The strict equality operator considers operands of different types not equal (see Strict Equality Comparison); it does not coerce.


On browsers, there's a third value that's == to null and undefined: document.all. document.all behaves like undefined in various specification operations. This is so that really, really old code that was using if (document.all) or similar to go an IE-specific direction doesn't do that on modern browsers, since the features that they were avoiding exit on IE versions that handle document.all this way. This is defined in the spec.

Upvotes: 16

Nate Whittaker
Nate Whittaker

Reputation: 1966

This behavior stems from JavaScript being a loosely-typed language. In the case of equality, this means values of differing data types can be compared.

Both null and undefined are considered "falsy", therefore they're loosely considered equal.

Logically, if null == false and undefined == false, then null == undefined.

Upvotes: -1

Lorenzo Hernandez
Lorenzo Hernandez

Reputation: 1

undefined means a variable has not been defined, or given a value. null is a special object within javascript.

Comparing the values using equals == will always give you true because they are both basically the same in value (0, nothing, nada, zip).

Using strict equals === though, will return false because null is an object and undefined is just a blank variable.

Upvotes: -1

Yaelle
Yaelle

Reputation: 391

If a variable is pointing to nothing, it's null. That's similar in concept to an undefined variable, which has not yet been initialized.

Javascript interpreted this to make loose equality (==) treat null and undefined the same way. Strict equality (===) is more picky; if you have different data types such as null and undefined, they won't be equal.

See What is the difference between null and undefined in JavaScript? on differences between undefined and null and Which equals operator (== vs ===) should be used in JavaScript comparisons?

Upvotes: -1

Spencer Wieczorek
Spencer Wieczorek

Reputation: 21575

The specifications for the == operator is defined so that null == undefined returns true.

Upvotes: 0

Related Questions