Javascript null vs undefined: The Complete Guide

In programming, software developers often have to deal with different data types. However, not all the time, the data is available to the programmer. The data which is not available to programmers are often called missing values.

In such cases, the programmer must encounter something different than the required value instead of the actual values. There can be many such instances related to it as follows:

  1. The value is unknown and hasn’t been entered into the database.
  2. The value is known but got corrupted etc.

In Javascript, the programmers also have to deal with such situations; hence, some terms are related to the same, like Null, None, void, undefined, etc.

In this article, we will discuss two such terms, undefined and null. It is essential for a novice to understand the differences between null and undefined and when to use what.

JavaScript null is an object, whereas undefined is a type. Therefore, the null and undefined are falsy values.

There are six primitive values in JavaScript, including null and undefined.

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

Difference between ==  and === in JavaScript

The main difference between == and === is that the  == operator first tries to convert the data types that need to be checked whether they are equal or not, whereas ===, on the other hand, is strict equality. Therefore, it doesn’t attempt to convert both the objects to the same data types.

Example 1

let a=9;

let b='9';

console.log(a==b);

Output

true

The ===, on the other hand, is strict equality. It doesn’t attempt to convert both the objects to the same data types.

Example 2

let a=9;

let b='9';

console.log(a===b);

Output

false

What is Null in JavaScript?

JavaScript null is an assignment value that you can assign to a variable. The null means nothing,, and it is one of the primitive values of JavaScript.

Sometimes the values are not entered into the database with reasons or some intention. These values are known to the user but are not entered. Hence, in place of the value, the value that is entered is usually called null

As a beginner, you should understand the below features of null:

  •  null is an empty or non-existent value
  •  null must be assigned

The null is also referred to as false.

Example

let a = null;

console.log(a);

Output

null

What is Undefined in JavaScript?

JavaScript undefined means a variable has been declared, but no value has been assigned.

Example 1

let a;

if (a == undefined) {

 console.log("It is undefined");

}

else {

 console.log('Is is not undefined');

}

Output

It is undefined

Example 2

let a=9;

if(a==undefined){

    console.log("It is undefined");

}

else{

    console.log('Is is not undefined');

}

Output

It is not undefined

JavaScript null vs undefined

The main difference between JavaScript null and undefined is that null is an intentional absence of value, whereas undefined is the value that does not exist in the compiler. The undefined is a global object.

The null and undefined have the following relationship:

1.On == operator it gives true.

2.On === operator it gives false.

This means since both have the same condition, they do not hold any value in the javascript while applying the == operator will try to convert both the data types as the same. Since both represent no value, the operator will give true as the truth value.

On the other hand, === is a strict comparison operator; hence, the javascript will return false as the truth value.

That’s it for the JavaScript null vs undefined tutorial.

Related posts

JavaScript let vs var

JavaScript vs TypeScript

JavaScript import vs require

Leave a Comment