JavaScript null and undefined

JavaScript is different from other programming languages in terms of data types, it is dynamically typed, allows changing the data type of a variable during runtime, it uses the concept of loose equality, i.e, it compares value regardless of what data type is, even it allows to declare a variable without any keyword like var, let, and const. Besides all of them, another unique thing about JavaScript is null And undefined.

In JavaScript, there are mainly 6 primitive data types:

  1. Number: It is a Numeric value(i.e integer, float, etc).
  2. Boolean: It is True or False.
  3. String: It is a sequence of characters.
  4. Symbol: It is a unique identifier.
  5. null: It is a special value that represents the absence of a variable.
  6. undefined: It is a variable that is declared but has no value.

In this tutorial, you will learn about null and undefined, distinct yet powerful data types.

Also Read: JavaScript Array: Understanding the Concept and Uses

JavaScript null

null is considered an object, that is used to explicitly assigned to a variable to indicate that the variable does not contain any value yet but it can be assigned to it later.

Developers, when creating a program, intentionally assigned null to a variable, to indicate that the variable is intentionally empty or blank.

For instance, you requested to access some data from an API, and if the data is not present, you can get a null return. In means, it is intentionally programmed to send users null, if the values he is searching for do not exist.

Example:

function sum(num1, num2){
    if(typeof num1 !== 'number' || typeof num2 !== 'number'){
        return null;
    }
    return num1 + num2;
}

result = sum(1, '2');

console.log(result);

Output:

null

Data Type of null

You can check the data type of null variables by using the typeof() method.

Example:

const x = null;

const result = typeof(x);
console.log(result);

Output:

object

Here you can notice, the data type of the null variable is an object, as null is considered an object in JavaScript whose value is not-exist.

The typeof() method just returns ‘object’, not the exact object type, we have a separate tutorial on How To Get Type Of Object In JavaScript in which we have covered how you can find the type of object if you want to read.

JavaScript undefined

undefined is used to represent the absence of a value. When a variable is defined but not assigned a value, JavaScript automatically assigned undefined to it.

Developers, when creating a program, used this to check whether a variable is assigned to a value or not, if not then the new value can be assigned.

Example:

var result;

if(result == undefined){
    console.log('result is undefined');
}

Output:

result is undefined

Data Type of undefined

You can also check the data type of undefined variables by using the typeof() method.

Example:

var x;

const result = typeof(x);
console.log(result);

Output:

undefined

Here you can see, the data type of undefined is also undefined.

Difference Between null and undefined

null and undefined are two sides of a coin, they are completely unique to each other however if you try to compare the values using the equality operator “==” they are the same.

var x = null;
var y;

if(x == y){
    console.log('x and y are equal');
}

Output:

x and y are equal

The fact that javascript compares only the value using the “==” operator, doesn’t mean they are the same, their values are the same as they are false by default.

var x = null;
var y;

console.log("Value of x: " + !!x + " and y: " + !!y);

Output:

Value of x: false and y: false

In the strict equality operator “===”, they are not considered the same, as it also checks for the type and both have a different data type.

var x = null;
var y;

if(x === y){
    console.log('x and y are equal');   
} else {
    console.log('x and y are not equal');
}

Output:

x and y are not equal

Summary

In JavaScript, if you declared a variable without any value, JavaScript automatically assigns undefined to it, on the other hand, null is intentionally assigned by a program to define that the value of a variable is blank. Hope this tutorial helps you to understand the concept of null and undefined in JavaScript.

Reference

https://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript

Aditya Gupta
Aditya Gupta
Articles: 109