JS: null

By Xah Lee. Date: . Last updated: .

What is null

typeof null

typeof null return "object". (this is a historical bug. typeof null should return "null" )

console.assert(typeof null === "object");

Why “typeof null” return "object"

typeof null return "object" is a historical implementation bug. Now we are stuck with it.

null is not a object, because It doesn't have the defining quality of JavaScript objects, namely, it's not a collection of key-and-value pairs; you cannot add properties to null.

try {
 null.p = 4;
} catch (xerror) {
 console.log(xerror);
}
// TypeError: Cannot set properties of null (setting 'p')

What is the Use of null

There are 2 major uses of null.

JavaScript. Special Literals