JavaScript Null vs Undefined

JavaScript – Null vs Undefined

Introduction

Null and Undefined are both data types in JavaScript. They both represent an empty value. Undefined is a declared variable but not assigned a value whereas Null means an empty or non-existent value.

In JavaScript, there are six primitive values. Both null and undefined are primitive values. Here is a full list:

  • Boolean
  • Null
  • Undefined
  • Number
  • String
  • Symbol

Type of Null and Undefined

typeof(null)

Output

object
typeof(undefined)

Output

undefined

We can see that the type of null is an object but the type of undefined is undefined.

Comparison between null and undefined

If we compare both the data types we can see that if we use strict equality (===) then the result is false but if we compare them with abstract equality (==) then the result is true.

null===undefined

Output

false
null==undefined

Output

true

Summary

  • null and undefined are falsy values.
  • null !== undefined but null == undefined.
  • null and undefined are both primitives. However, an error shows that typeof null = object.