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

Updated on: 02-Jan-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started