JavaScript Basics

JavaScript Advanced

JavaScript Arrays

JavaScript Functions

JavaScript Objects

JavaScript DOM

JavaScript String

What is null in JavaScript?

null is one of the primitive data types in JavaScript.

null is used to show that the variable does not have any object value. In simple terms, null represents the absence of value.

JavaScript does not assign null to the variable. Instead, it is the programmer who assigns null to the variable to show that the object has no value.

null is one of the falsy values in JavaScript. This means if you use null in conditionals inside the if statement, then it will be treated as false.

let x = null;
  console.log("Variable x is not null.");
  console.log("Variable x is null.");


Variable x is null

What is typeof null in JavaScript

The typeof operator tells you the type of value.

It is interesting to note that when the typeof operator is used with null it returns "object". So, you might think, why does the typeof operator return "object" for null? The straightforward answer to this question is that this could be a mistake by the early implementors of JavaScript. Because of which it is advised that you do not use the typeof operator to determine if a variable is null or not. Instead, use a strict equality operator (====).

let x = null;
let name = {
  firstname: 'Mohit',
  lastname: 'Natani'
console.log(typeof x); //"object"
console.log(typeof name); //"object"
if(x === null){
  console.log("Variable x is null.");
  console.log("Variable x is not null.");

In this example, you can see that the typeof operator returns "object" for variable x and object name.

Recommended Posts