JavaScript

JavaScript isnull | Explained

In JavaScript, the null value represents a value that is explicitly absent from an object. Along with undefined, boolean, integer, and string, the null is the JavaScript primitive value. The null value is frequently used to signify the absence of a value or an object. For instance, if you attempt to access a property of a non-existent object, the result will be null.

This post will explain the “isnull” in JavaScript.

How to Perform “isnull” Functionality in JavaScript?

The “isnull” is not a built-in function in JavaScript, but you can check if a variable is null by using the following ways:

  • Strict equality operator (===)
  • typeof operator

Method 1: Check if a Value is null Using the Strict Equality Operator

The strict equality operator “(===)” is a comparison operator in JavaScript that compares the value and the type of two variables. It outputs true if the values and types are the same otherwise, it returns false.

Example

Create a variable “value” that stores value “null”:

let value = null;

Check value using the strict equality operator. It will give “true” if the value and the type both are equal:

if (value === null) {
 console.log("value is null");
}
else{
 console.log("value is not null");
}

The output indicates that the value is null as it returns “true” by comparing by value and the type:

Now, we will create an “isNull()” function to check the value of the variable using strict equality operator (===):

function isNull(value) {
 if ( value === null ){
  return true;
}
 else
  return false;
}

Create two variables “val1” and “val2” by assigning values “15” and “null”, respectively:

var val1 = null;
var val2 = 15;

Check the values are null or not by calling the “isNull()” function:

console.log("Value 1 is null? " + isNull(val1));
console.log("Value 2 is null? " + isNull(val2));

It can be seen that the “val1” is null as it returns “true” and the “val2” is not null as it gives “false”:

Method 2: Check if a Value is null Using the “typeof” Operator

To verify if the value is null or not, you can also use the “typeof” operator. It is used to verify the variable or an expression’s type. It returns a string indicating the type of the operand, such as “undefined” for undefined variables, “string” for strings, “boolean” for boolean, and so on.

Example

Check the type of variable in the conditional statement using the “typeof” operator with strict equality operator:

if (typeof value === "object" && value === null) {
 console.log("value is null");
}
else{
 console.log("value is not null");
}

Output

That’s all about the isnull in JavaScript.

Conclusion

In JavaScript, the null value represents no value or no object. The “isnull” is not a built-in function in JavaScript, but you can check if a variable is null by using the “strict equality” operator (===)” or the “typeof” operator. The commonly used approach is the strict equality operator. In this post, we explained the isnull functionality in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.