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”:
Check value using the strict equality operator. It will give “true” if the value and the type both are equal:
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 (===):
if ( value === null ){
return true;
}
else
return false;
}
Create two variables “val1” and “val2” by assigning values “15” and “null”, respectively:
var val2 = 15;
Check the values are null or not by calling the “isNull()” function:
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:
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.