Karthikeyan's Blog

Karthikeyan's Blog

Javascript Shorts : null and undefined

Javascript Shorts : null and undefined

Karthikeyan's photo
·May 12, 2022·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

  • What is null ?
  • What is undefined ?
  • Why typeof null is an object ?
  • Interesting facts on null and undefined :
  • Conclusion
  • References and Resources:

Hello everyone, In this blog we will be seeing about the javascript’s two famous primitives null and undefined.

What is null ?

primitive value that represents the intentional absence of any object value. - ECMA - 262

  • javascript never assigns a null value unless and until it is intentionally assigned.
  • null is considered as a falsy value as it represents there is no value present.

let a = null;

console.log(a) // null
console.log(typeof null) // object
console.log(!!a) // false

What is undefined ?

primitive when a variable has not been assigned any value. - ECMA - 262

undefined is where we declare a variable but not assign any value to it.

let a;

console.log(a) // undefined
console.log(typeof a) // "undefined"

Why typeof null is an object ?

let’s try to understand why when we log the typeof undefined we get undefined and when we log the typeof null we get an object . Since null is a primitive value with a primitive type Null why not the type as Null .


Typeof operator returns the type of variable in the form of string.

typeof null === 'object';

In the earlier implementation of javascript, values are stored in 32bit units. first three bits ( 1 - 3 ) bits for identifying the type of the value and rest of 29 bits contained the actual value.

There was 5 type tags in which object had type tag of 000. In most of the platforms , null was represented as the NULL pointer which ( 0x00 ). This loosely translates that null also has 0 type tag which is type of "object”.

More info with code on the typeof null : reference

MDN Reference

In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. null was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the typeof return value "object". (reference)

A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in typeof null === 'null'.

Interesting facts on null and undefined :

  1. they both are equal and not equal. What do I mean by that
console.log(null === undefined) // false

When we use javascript’s strict equality operator to check we will get false as the type of both null and undefined are different.

console.log(null == undefined) // true

Language spec says that when we check for similarities between null and undefined using loose equality operator it is should be true.

  1. what if I try to add null and number. null gets converted to 0 resulting a the number
console.log(1+null) // 1
  1. When we try to add undefined and number we will get NaN.
console.log( 1 + undefined ) // NaN


That's pretty much it. Thank you for taking the time to read the blog post. If you found the post useful , add ❤️ to it and let me know in the comment section if I have missed something.

Feedback on the blog is most welcome.

Social Links:

Twitter Showwcase

References and Resources:

  1. null vs undefined
  2. loose equality comparison
  3. Falsy - MDN docs
Share this