Difference b/w null and undefined in javascript

Arvind Dhakar
1 min readApr 10, 2021
Photo by Florian Olivo on Unsplash

Before we discuss differences let have some common characteristics.

Common characteristics

  1. Both are primitive data types
  2. Both are falsy values.[If you don’t know falsy values in javascript, just google it]
  3. Both are used as value and also as a type means [if x is a typeof ‘undefined’ then its value must be undefined]
  4. Both are used for variables that values do not exist.[Interviewer will ask here then what is the difference b/w both of them]

A noob interviewee answer

  • typeof null = ‘object’[due to legacy reasons] & typeof undefined = ‘undefined’ 😂

An experienced interview answer

  1. Javascript never implicitly assigns null value to any variable but does implicitly assigns undefined value when it finds variable is declared but not initialized.
  2. In case we want to explicitly assign a variable empty value or non-existence value then we must assign it null value. We can assign it undefined but it doesn’t make any sense.

null value
primitive value that represents the intentional absence of any object value.

undefined value
primitive value used when a variable has not been assigned a value.

--

--