Javascript – What is the difference between undefined and null ?

In this article, we’ll see what is the difference between undefined and null in javascript.

undefined: means a variable has been declared but has not yet been assigned a value.

Below an example:

var UndefinedVar;
console.log(UndefinedVar); //shows undefined
console.log(typeof UndefinedVar); //shows undefined

null: means an empty or non-existent value. It can be assigned to a variable as a representation of no value.

Below an example:

var NullVar = null;
console.log(NullVar); //shows null
console.log(typeof NullVar); //shows object




In above examples, we’ve seen that undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.

null === undefined // false
null == undefined // true
null === null // true

Here are the quick facts:

  • null is an assigned value. It means nothing.
  • undefined means a variable has been declared but not defined yet
  • null is an object. undefined is of type undefined.
  • null !== undefined but null == undefined.

That’s it!. Please share your thoughts or suggestions in the comments below.

2 thoughts on “Javascript – What is the difference between undefined and null ?

Leave a Reply

Your email address will not be published. Required fields are marked *