How do I check for null values in JavaScript?


In this article, we will learn how to check the null values in JavaScript. The null value indicates the intentional absence of any object value. It is a JavaScript primitive value that is false when used in Boolean operations. This distinguishes null from the related primitive value undefined, which is an unintended absence of any object value. This is because a variable that has been declared but not given a value is undefined rather than null.

You may visualize a variable as a box by using a real-world analogy. The box can hold things like a teapot, just like a variable can. However, when you open a box you receive and find nothing inside! You received an empty package because someone made a mistake. The box has a null value, which is another way of stating it contains nothing.

There are different ways to check whether a value is null in JavaScript.

  • Using the Strict equality Operator (===)
  • Using the Object.is() Function
  • Using the typeof Operator

Using the Strict equality Operator (===)

In this approach, we will learn how to use the (===) operator to check for null values. This test will only pass for null and not for "", undefined, false, 0, or NaN.

Syntax

Following is the syntax to use strict equality operator −

let result = ( v === null );

Return values

  • v === null − This parameter checks whether the value stored in variable “v” is equal to null or not. Accordingly, it returns true or false.

Algorithm

  • STEP 1 − We see a variable v is declared and initialized to a null value.

  • STEP 2 − Then we use an if-statement to check if the variable is null and if true an appropriate message is displayed on the user's screen.

Example

The below example demonstrates how to use the strict equality operator (===) to check for null values in JavaScript.

<!DOCTYPE html>
<html>
<head>
   <title> Check for null values in JavaScript </title>
</head>
<body>
   <h2>Check for null values using the strict equality operator.</h2>
   <div id="output"> </div>
   <script>
      let output = document.getElementById("output");
      var v = null;
      if (v === null) {
         output.innerHTML = "Value is null";
      } else {
         output.innerHTML = "Value is not null";
      }
   </script>
</body>
</html>

In the above output, the variable is being checked for null and the value is being executed in the if-block that the variable contains a null value.

Using the Object.is() Function

The Object.is() function in JavaScript that compares two values to see whether they are the same. A boolean value indicates if the two parameters in the function have the same value. Two values could be the same if both the values are null.

Syntax

Following is the syntax for the Object.is() function −

Object.is( q, null );

Parameters

  • q − variable to be passed inside the function which is initialized with a null value.

  • null − this parameter is a null value.

Algorithm

  • STEP 1 − We declare a variable called q and set it to null.

  • STEP 2 − Then, given the inputs q and a null value, we check the Object.is() a method with an if-statement to determine whether both are the same.

  • STEP 3 − If they are the same values an appropriate message being displayed on the user's screen.

Example

In the program below, we use the Object.is() function to check for null values.

<!DOCTYPE html>
<html>
<head>
   <title>Check for null values in JavaScript</title>
</head>
<body>
   <h2>Check for null values using the JavaScript Object.is() method.</h2>
   <div id="output"> </div>
   <script>
      let output = document.getElementById("output");
      var q = null;
      if (Object.is(q, null)) {
         output.innerHTML = "Value is null";
      } else {
         output.innerHTML = "Value is not null";
      }
   </script>
</body>
</html>

In the above output, the variable is being checked for null and the value is being printed with a true statement that the variable contains a null value.

Using the typeof Operator

The typeof operator may be used to determine the data type of a JavaScript variable. Here we use the typeof operator with the null operator. The (!variable) means the value is not null and if it is checked with the typeof operator variable which has the data type of an object then the value is null.

Syntax

Following is the syntax to typeof operator −

typeof var;

Here var is the variable whose data type is to be checked and is used as a parameter.

Algorithm

  • STEP 1 − We declare a variable called a set it to null.

  • STEP 2 − Then we check whether the complement of a variable and the variable's data type is an object or not.

  • STEP 3 − If both conditions match, then the value is deemed equal to null.

  • STEP 4 − An appropriate message is displayed on the user's screen.

Example

In this example, we use the JavaScript typeof operator to check whether the values of a given variable are equal to null or not.

<!DOCTYPE html>
<html>
<head>
   <title>Check for null values in JavaScript </title>
</head>
<body>
   <h2>Check for null values using the JavaScript typeOf operator.</h2>
   <div id="output"> </div>
   <script>
      let output = document.getElementById("output");
      var a = null;
      if (!a && typeof a === "object") {
         output.innerHTML = "Value is null";
      } else {
         output.innerHTML = "Value is not null";
      }
   </script>
</body>
</html>

The variable is tested for null in the above output, and the value is executed in the if-block that the variable has a null value.

Conclusion

In this tutorial, we used three approaches to check null values in JavaScript.

In the first approach, we used the strict equality operator and showed that two null values could be compared.

The second approach is beneficial as it uses the Object.is() function to check and compare two null values.

The third approach is by using the typeof operator which checks the value of the variable and its data type to return whether it is a null value.

All these approaches in the article sum up how to check for null values in JavaScript.

Updated on: 22-Jul-2022

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements