What is the difference between null and undefined in JavaScript?

In JavaScript, undefined is a type, whereas null an object.


It means a variable declared, but no value has been assigned a value.

For example,

var demo;
alert(demo); //shows undefined
alert(typeof demo); //shows undefined


Whereas, null in JavaScript is an assignment value. You can assign it to a variable.

For example,

var demo = null;
alert(demo); //shows null
alert(typeof demo); //shows object