JavaScript null and undefined

There are 8 types of data types in JavaScript. They are:

Data Types Description
String represents textual data
Number an integer or a floating-point number
BigInt an integer with arbitrary precision
Boolean Any of two values: true or false
Object key-value pairs of collection of data
Symbol a data type whose instances are unique and immutable
undefined a data type whose variable is not initialized
null special keyword denoting a null value

undefined and null are the two data types that we will discuss in this tutorial.


JavaScript undefined

If a variable is declared but the value is not assigned, then the value of that variable will be undefined. For example,

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

It is also possible to explicitly assign undefined to a variable. For example,

let name = "Felix";

// assigning undefined to the name variable
name = undefined

console.log(name); // returns undefined

Note: Usually, null is used to assign 'unknown' or 'empty' value to a variable. Hence, you can assign null to a variable.


JavaScript null

In JavaScript, null is a special value that represents an empty or unknown value. For example,

let number = null;

The code above suggests that the number variable is empty at the moment and may have a value later.

Note: null is not the same as NULL or Null.


False Values

In JavaScript, undefined and null are treated as false values. For example,

if(null || undefined ) {
    console.log('null is true');
} else {
    console.log('null is false');
}

Output

null is false

An undefined or null gets converted to false when used with the Boolean() function. For example,

let result;

result = Boolean(undefined);
console.log(result); // false

result = Boolean(null);
console.log(result); // false

JavaScript typeof: null and undefined

In JavaScript, null is treated as an object. You can check this using the typeof operator. The typeof operator determines the type of variables and values. For example,

const a = null;
console.log(typeof a); // object

When the typeof operator is used to determine the undefined value, it returns undefined. For example,

let a;
console.log(typeof a); // undefined

JavaScript Default Values: null and undefined

Before you visit this section, be sure to check the JavaScript default parameter tutorial.

In JavaScript, when you pass undefined to a function parameter that takes a default value, the undefined is ignored and the default value is used. For example,

function test(x = 1) {
  console.log(x);
}

// passing undefined
// takes default value 1
test(undefined); // 1

However, when you pass null to a default parameter function, the function takes the null as a value. For example,

function test(x = 1) {
  console.log(x);
}

// passing undefined
// takes null
test(null); // null

Comparing null and undefined

When comparing null and undefined with equal to operator ==, they are considered equal. For example,

console.log(null == undefined); // true

In JavaScript, == compares values by performing type conversion. Both null and undefined return false. Hence, null and undefined are considered equal.

However, when comparing null and undefined with strict equal to operator ===, the result is false. For example,

console.log(null === undefined); // false
Did you find this article helpful?