JavaScript: null Value

Profile picture for user arilio666

A pot that has nothing in it is called an empty pot whereas if we want to fill it with water later it's not empty anymore!

The same logic applies to javascript null value.

  • If a variable already has a value it can be explicitly cleared off and made null; which means it is empty or the variable is not assigned with any value at the moment.
  • We can also assign null to a variable to denote that the variable is purposefully empty at the moment but later can be assigned.

Example:

var a;                        //Undefined
console.log(a);
a = true;                   //Defined
console.log(a);
a = null;                   //Made Null
console.log(a);

Output:

Undefined
true
null
  • So basically in first step, we have defined variable a, which when logged will throws an undefined. Undefined is something that occurs when there is no value to the variable.
  • After that, we have given a boolean value true to 'a' where it prints the value true.
  • During 3rd assignment, We explicitly want the variable 'a' to be empty but not undefined so we have given it a value null. Specifically means that we want the boolean value to be removed.