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?
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?
 
    
     
    
    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.
 
    
    The specifications for the == operator is defined so that null == undefined returns true.
 
    
    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.
 
    
    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?
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.
