JavaScript - Null Checking


In this chapter, 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.

We can take an example of a box that has nothing in it, nothing at all. We can say that the box is null. Similarly, in JavaScript, we can say that a variable is null when it has no value.

Checking Null Values

We can check the null values in javascript using quite a few methods. Let's see them one by one.

  • Using the Strict equality operator (===)
  • Using the typeof operator
  • Using Object.js method

Using the Strict equality operator (===)

In this method, we will see how we can check null values using the strict equality operator (===). The strict equality operator compares two values for equality. It returns true if the values are equal and false otherwise. It is a strict equality operator, which means it checks both the type and the value of the variable.

Syntax

The syntax for checking null values using the strict equality operator is given below.

let result = ( v === null );

Example

Below is the example of checking 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 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.

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 typeof operator.</h2>
<div id="output"></div>
<script>
let output = document.getElementById("output");
var v = null;
if (!v && typeof v === "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.

Using Object.js method

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 to Object.is() method

Object.is( q, null );

Parameters

The parameters used in the Object.is() method are as follows

  • q The first value to compare.
  • null The second value to compare.

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 Object.is() method.</h2>
<div id="output"></div>
<script>
let output = document.getElementById("output");
var v = null;
if (Object.is(v, null)) {
   output.innerHTML = "Value is null";
} else {
   output.innerHTML = "Value is not null";
}
</script>
</body>
</html>

Output

Following is the output of the above program.

Value is null

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.

Conclusion

In this chapter, we have learned how to check null values in JavaScript using the strict equality operator, typeof operator, and Object.is() method. We have also seen the syntax and examples of each method.

Advertisements