Null indicates the absence of any object value. It signifies that a variable does not have a value assigned to it. While null and undefined are both primitive types in JavaScript, they have distinct characteristics. In this article, we will look into the NULL in JavaScript and how to check for the null value in variables. Let us get started.
What is Null in JavaScript?
In JavaScript, null represents the intentional absence of any object value. When a variable is intentionally left without a value, it is assigned the value of null (i.e. an empty object). It is a primitive data type and a keyword in the language.
Let us see an example of the same:
//declaring a variable with a null value var num=null; //printing the variable console.log(num);
Output:
null
In this example, we have declared a variable with a Null value. We have printed the same and got the desired output.
Let us check the type of the variable.
//declaring a variable with a null value var num=null; //checking the type of variable console.log(typeof(num));
Output:
object
In this example, we have declared a variable and assigned it a null value. We checked the type of the null value using the typeof operator. The output is “object”, which means that null is an empty object. We have obtained the desired result.
On the other hand, an undefined variable refers to a variable that has been declared but has not been assigned any value.
3 Methods to Check for Null in JavaScript
In this section, we will look into various methods to check for a null value.
1) Using the Strict Equality Operator(===)
We can use the strict equality operator (===) in JavaScript to check if a variable is explicitly set to null. It not only checks for the value of the two variables to be equal but also checks for their types to be the same.
Let us see an example:
//declaring a variable with a null value var num=null; //checking for the null value if(num===null){ console.log("The variable holds a null value"); } else{ console.log("The variable doesn't hold a null value") }
Output:
The variable holds a null value
We have declared a variable with a null value and checked using the strict equality operator if the variable holds a null value or not. We have obtained the desired result.
2) Using the Object.is() Function
Another method to check for null values is by using the Object.is() function. It compares two values. The function returns true if the two values are the same and false if they are not. Let us see an example:
//declaring a variable with a null value var num=null; //checking for the null value using the Object.is() if(Object.is(num, null)){ console.log("The variable holds a null value"); } else{ console.log("The variable doesn't hold a null value") }
Output
The variable holds a null value
It is a concise way to check for null values in JavaScript. It compares the variable with a null value. If the variable holds a null value, it returns true else returns false.
3) Using the typeof Operator
The typeof operator in JavaScript determines the data type of a variable. We can combine the typeof operator and a conditional statement to check if a variable holds a null value. Here is how to do it:
//declaring a variable with a null value var num=null; //checking for the null value using the typeof Operator if(typeof(num)==="object" && num===null){ console.log("The variable holds a null value"); } else{ console.log("The variable doesn't hold a null value") }
Output:
The variable holds a null value
The variables having null values are of the type Object. In this example, we use the typeof operator to check if the type of variable is an Object and it holds a null value. If both conditions are satisfied, then the variable holds a null value. Thus, we have obtained the desired result.
You can also check for undefined in Javascript using typeof operator.
Conclusion
In JavaScript, It is essential to understand how to check for the null variables to write robust and clean code. In this article, we first discussed various techniques to check for the null value. If you have any more doubts, our experts can assist you in any type of JavaScript assignment help online anytime.