undefined
is the value you get for variables and properties which have not yet been created. Use the same value to reset an existing
variable and you lose the ability to distinguish between a variable that exists but has no value and a variable that does not yet exist. Instead,
null
should be used, allowing you to tell the difference between a property that has been reset and one that was never created.
Noncompliant code example
var myObject = {};
// ...
myObject.fname = undefined; // Noncompliant
// ...
if (myObject.lname == undefined) {
// property not yet created
}
if (myObject.fname == undefined) {
// no real way of knowing the true state of myObject.fname
}
Compliant solution
var myObject = {};
// ...
myObject.fname = null;
// ...
if (myObject.lname == undefined) {
// property not yet created
}
if (myObject.fname == undefined) {
// no real way of knowing the true state of myObject.fname
}