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;
Check out the reference here instead of relying on people dismissively saying junk like "In summary, undefined is a JavaScript-specific mess, which confuses everyone". Just because you are confused, it does not mean that it is a mess.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined
This behaviour is also not specific to JavaScript and it completes the generalised concept that a boolean result can be true
, false
, unknown (null
), no value (undefined
), or something went wrong (error
).