undefined and null

suggest change

At first glance it may appear that null and undefined are basically the same, however there are subtle but important differences.

undefined is the absence of a value in the compiler, because where it should be a value, there hasn’t been put one, like the case of an unassigned variable.

Setting a variable to undefined means the variable effectively does not exist. Some processes, such as JSON serialization, may strip undefined properties from objects. In contrast, null properties indicate will be preserved so you can explicitly convey the concept of an “empty” property.

The following evaluate to undefined:

undefined is also a property of the global window object.

// Only in browsers
console.log(window.undefined); // undefined
window.hasOwnProperty('undefined'); // true

Before ECMAScript 5 you could actually change the value of the window.undefined property to any other value potentially breaking everything.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents