Undefined vs Null: What are the differences and similarities between undefined and null?

Undefined vs Null: What are the differences and similarities between undefined and null?

As a Javascript developer you may have encountered the terms undefined and null and may have wondered what really is their difference as they appear to be the same. Yes they appear to be the same and appear at similar instances but are not the same thing.

This blog post explains the differences, uses and similarities between undefined and null and gives examples where necessary for better understanding. This is amongst the most frequently asked questions during Javascript interviews and is also a key element for any Javascript developer to note.

Differences

1. General Descriptions

The best way to differentiate undefined and null is through their definitions/general descriptions according to Javascript.

undefined is a data type that Javascript automatically sets to a variable that has been declared but no value has been assigned to it.

var name; console.log(name); //undefined

undefined can also be assigned to a variable by the user. var name = undefined; console.log(name); //undefined

null is also a primitive data type that a user can assign a variable to. null indicates that the variable is empty. The user can later reassign the variable to another value later on in the program.

var name = null; console.log(name); //null

Note that Javascript can never automatically set the value of a variable to null; only the user can assign this value unlike undefined which is set when the variable has no value assigned.

2. typeof

The typeof operator returns the data type of a variable.

undefined and null return different typeof values.

undefined returns "undefined" as it is of the undefined primitive data type while null returns "object".

However, in the case of null, it should return null as null is already a Javascript primitive data type. It returns object due to an existent Javascript bug that has been passed on throughout the years.

```var name; console.log(typeof name ); //"undefined"

var age=null; console.log(typeof age ); //"object"```

3. Strict equality

null === undefined //returns false

null == undefined //returns true

Comparing null and undefined using the strict equality operator returns false as the two are of different data types.

On the other hand, comparing the two using the loose equality operator returns true as both are falsy values. ( this should have been on the similarities tab.)

4. Arithmetic Operations

According to Javascript undefined is not-a-number(NaN) while null is considered a number while performing arithmetic operations.

Hence, an arithmetic operation involving undefined returns NaN or concatenates it if it's addition.

Example:

var p = 100; var r= 0.01; var t; var interest = p*r*t; console.log(interest); //NaN

var age=null; var father= 50 + age; console.log(father); // 50

Similarities

  1. Both are falsy values- Both undefined and null return false.
  2. Both are amongst the Javscript primitive data types.
  3. Both have the general meaning of nothing/empty.