The other day I was explaining to someone about the differences between null
and undefined
in Javascript.
I thought it might be worth writing a quick post to show how Javascript handles each one. The undefined
type
is quirky feature of javascript that is similar to null
but different.
Differences between null
and undefined
let a;
a == null // true
a === null // false
a == undefined // true
a === undefined // true
let b = null;
b == null // true
b === null // true
b == undefined // true
b === undefined // false
typeof a // 'undefined'
typeof b // 'object'
let z = {c: null};
c.notFake == null // true
c.notFake === null // true
c.notFake == undefined // true
c.notFake === undefined // false
d.fake == null // true
d.fake === null // false
d.fake == undefined // true
d.fake === undefined // true
null = 1; // throws error: invalid assignment
undefined = 1; // works fine but could be problematic
Why are they different?
Javascript has six built in types of which null
and undefined
are two. Variables that have yet to be
assigned are assigned undefined
. Null values on the other hand represent values where there is an intentional
absence of an assigned value. Thus both represent the absence of a value but one is intentional (null
) and
one is not (undefined
). For this reason the following holds true:
undefined === null; // false
undefined == null; // true
let x = (function(){
// ...returns nothing
})();
x === undefined; // true
x === null; // false
function someFunc(arg1, arg2) {
console.log(arg1, arg2);
}
someFunc("test"); // prints -> "test", undefined
N.B This example shows why it is better to use triple equals (===
) to ensure the robustness of your program. Double equals
(==
) will lead to less code but also more potential issues.
Does assigning undefined
delete a property from an object?
In short, “no”. You can assign undefined
to a property but it won’t remove it from an object.
The only way to remove a property from an object is to use delete yourobject.property
. delete
is intended to be
used on objects. Although pre ES5 allowed delete someVariable
to be used on variables, ES5+ in strict mode won’t allow this.
Reading more
undefined
on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined
undefined
value as defined by the ECMASScript specification: https://www.ecma-international.org/ecma-262/9.0/index.html#sec-undefined-value
undefined
type as defined by the ECMASScript specification: https://www.ecma-international.org/ecma-262/9.0/index.html#sec-terms-and-definitions-undefined-type
null
on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null
null
value as defined by the ECMASScript specification: https://www.ecma-international.org/ecma-262/9.0/index.html#sec-null-value
null
type as defined by the ECMASScript specification: https://www.ecma-international.org/ecma-262/9.0/index.html#sec-terms-and-definitions-null-type
The six types of Javascript: https://www.ecma-international.org/ecma-262/9.0/index.html#sec-ecmascript-data-types-and-values
« Javascript Debugging
ES6 breaking your Jest tests? »