Reliable and Recognized JavaScript Certification Online #

Null type has also only one value that is the special value null. The null is a keyword in JavaScript.
The null indicates absence of value, no value for objects, numbers and strings. It can be considered as an empty object pointer.

If a variable is meant to later hold an object it is recommended to initialise it to null.
Also when an object, string or number is expected and is not available, instead of it null can be used.

// initialize variable to null
var product = null;
// assign object
if (product === null) {
    product = {name: 'chocolate'};

var amount;
var isDataAvailable = false;
if (!isDataAvailable) {
    // there is no data available to calculate amount and null is set
    amount = null;

null and undefined are similar and both mean an absence of value. They are considered equal by the equality operator (==):

console.log(null == undefined); // true

To distinguish them the strict equality operator (===) should be used.

console.log(null === undefined); // false

The main difference between them is that undefined represents unexpected absence of value and the value of a variable should not be set to undefined. null however represents normal, expected absence of value and if there is no value, the value of variable can be set to null.

No Comments