Null
and undefined
in javaScript seems the same but there is a significant difference between the two. In this article let us explore the similarities and differences between them.
Undefined
Undefined means no-value
. if you have declared a variable but did not initialize/assign it with any value,By default it stays undefined
, If you try to read a non-existing property in an object it gives undefined
. you can also assign a variable as undefined
.
let i;
console.log(i); //undefined
i={};
console.log(i.something); //undefined
i= undefined;
console.log(i); //undefined
Null
Null is non-existing value
or you can also say no object value
.A variable becomes null
only when you assign it as null
. so null
doesn't come by default.
let i;
console.log(i);// undefined
i = null;
console.log(i);// null
Both null and undefined evaluates to falsy
.
let i;
if(i){
console.log('truthy');
}
else{
console.log('falsy');
} //prints falsy
i=null;
if(i){
console.log('truthy');
}
else{
console.log('falsy');
} // prints falsy
as we can see both null
and undefined
evaluates to falsy
values.
But interestingly if we check the type of null
it gives object even though
both null
and undefined
are said to be primitives. This is regarded as
mistake in javaScript implementation.
console.log(typeof null); //object
conosle.log(typeof undefined); //undefined
Why typeof null
gives object
is explained in this v8 article.
following the above image Brendan Eich the (creator of javascript) designed JavaScript to make typeof return 'object' for all values on the right-hand side. That’s why typeof null
gives object
despite the spec having a separate Null type.
one more thing is when we compare null and undefined with ===
it returns false
but when we compare null == undefined
it returns true
because of type-coercion.
summarizing the post:
- undefined means
not-defined
. a variable isundefined
until you give it some value. - null means
no-value
. something becomes null only if it is made(assigned) null. - null and undefined both evaluate to falsy.
- null and undefined both are primitive values.
-
typeof null
givesobject
.
if you like my post please do like. you can also follow me here on dev.to or on twitter
Top comments (2)
Simple and non confusing explanation, thank you
Thank you for your kind words Jan!