Contact for Expert ServicesAdvertise with Ecomspark.

Difference between null and undefined in Javascript?

This is very common questions asked in JavaScript interviews and newbie confused most of the time, what is the difference between null and undefined.

Undefined – In JavaScript, Undefined means a variable is declared but has not been assigned any value.

For Example, Lets define a variable –

var a;
alert(a); // undefined
alert(typeof a); //undefined

As we can see in above example, variable a and typeof a is also undefined.

null – ┬ánull means empty or non-existent value. Null can be assigned to a variable as a representation of no value.

null is primitive value  not an object and can be assign to any variable. null returns typeof object but we can not add properties to it.

var a = null;
alert(a); // shows null
alert(typeof a); //shows object
null === undefined // false
 null == undefined // true
 null === null // true

Add a Comment

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

Translate »