Skip to main content

JavaScript: null Value

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.
Tags
Submitted by arilio666 on September 13, 2021

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.