Thursday, September 15, 2016

why undefined and null coexist in java script(Node js).

The basic difference is that undefined and null represent different concepts.
If only null was available, you would not be able to determine whether null was set intentionally as the value or whether the value has not been set yet unless you used cumbersome error catching: eg
var a;

a == null; // This is true
a == undefined; // This is true;
a === undefined; // This is true;
However, if you intentionally set the value to null, strict equality with undefined fails, thereby allowing you to differentiate between null and undefined values:
var b = null;
b == null; // This is true
b == undefined; // This is true;
b === undefined; // This is false;

basically if we just declare a variable in js its type is undefined
var b;
console.log(typeof b);// must print undefined

but if we declare a object and initialize with null its type is 'object' not undefined
var b = null;
console.log(typeof b) // must print object not undefined.

No comments:

Post a Comment