Javascript Shorts : null and undefined

imkarthikeyan

Karthikeyan

Posted on May 12, 2022

Javascript Shorts : null and undefined

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

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

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 .

https://media.giphy.com/media/gd09Y2Ptu7gsiPVUrv/giphy.gif

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

typeof null === 'object';
Enter fullscreen mode Exit fullscreen mode

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 0null 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 :

they both are equal and not equal. What do I mean by that

console.log(null === undefined) // false
Enter fullscreen mode Exit fullscreen mode

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

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


What if I try to add null and number. null gets converted to 0 resulting a the number

console.log(1+null) // 1
Enter fullscreen mode Exit fullscreen mode

When we try to add undefined and number we will get NaN.

console.log( 1 + undefined ) // NaN
Enter fullscreen mode Exit fullscreen mode

Conclusion

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
💖 💪 🙅 🚩
imkarthikeyan
Karthikeyan

Posted on May 12, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related