I want to know what the difference is between null
and undefined
in JavaScript.
OK, we may get confused when we hear about null
and undefined
, but let's start it simple, they both are falsy and similar in many ways, but weird part of JavaScript, make them a couple of significant differences, for example, typeof null
is 'object'
while typeof undefined
is 'undefined'
.
typeof null; //"object"
typeof undefined; //"undefined";
But if you check them with ==
as below, you see they are both falsy:
null==undefined; //true
Also you can assign null
to an object property or to a primitive, while undefined
can simply be achieved by not assigning to anything.
I create a quick image to show the differences for you at a glance.