According to What is the difference between null and undefined in JavaScript?, null
and undefined
are two different objects (having different types) in Javascript. But when I try this code
var a=null;
var b;
alert(a==null); // expecting true
alert(a==undefined); // expecting false
alert(b==null); // expecting false
alert(b==undefined); // expecting true
The output of the above code is:
true
true
true
true
Now as ==
only matches the value, I thought that both undefined
and null
must have the same value. So I tried:
alert(null)
-> gives null
alert(undefined)
-> gives undefined
I don't understand how is this possible.
Here is the demo.
Edit
I understand that ===
will give the expected result because undefined
and null
have different types, but how does type conversion work in Javascript in the case of ==
? Can we do explicit type conversion like we do in Java? I would like to apply a manual type conversion on undefined
and null
.