DEV Community

aravind_reddy
aravind_reddy

Posted on • Edited on

4 1

Null and undefined in JavaScript

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Why typeof null gives object is explained in this v8 article.

v8

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:

  1. undefined means not-defined. a variable is undefined until you give it some value.
  2. null means no-value. something becomes null only if it is made(assigned) null.
  3. null and undefined both evaluate to falsy.
  4. null and undefined both are primitive values.
  5. typeof null gives object.

if you like my post please do like. you can also follow me here on dev.to or on twitter

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (2)

Collapse
 
smil3cz profile image
Jan Melicherik

Simple and non confusing explanation, thank you

Collapse
 
reddyaravind178 profile image
aravind_reddy

Thank you for your kind words Jan!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay