JavaScript Program to Check for Null

Overview
In JavaScript programming language, there is a primitive data type known as 'null', which represents the empty value inside a variable. Although there is one more data type known as 'undefined', it does not represents the null value. In this article, we will discuss different methods to check for Null in JavaScript
Introduction
Null is a primitive type present in JavaScript which indicates the absence of any object value. When the value of a variable or an object is null, it indicates that the object value is absent in the variable. So, Null is often used at these places where an object can be expected, but no object is relevant.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
JavaScript Program to Check for Null Using typeof()
In this approach, we will use the typeof() method to find the type of an object or variable. The typeof method will return a string named 'object' if the variable passed as the parameter is null. Here we will use the !variable because applying ! in front of a null object will make the object not null. So, the if condition will become true.
JavaScript Implementation
Output
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Explanation
- In the first step, we created a variable x and initialized it with a null value.
- Inside the if condition, we have used the typeof() method to check the data type of x.
- The condition will be true if the value of typeof(x) will return true and the value of !x is also true.
- Finally, we have printed the result
JavaScript Program to Check for Null Using Equality Operator
In this approach, we will use the equality operator '==' to check if the variable contains null or not. If the value of x is null, then the statement inside the If block will be printed.
JavaScript Implementation
Output
Explanation
- In the first step, we created a variable x and initialized it with a null value.
- Inside the if condition, we are simply checking the value of x is null or not by using the equality operator '=='
- The condition will be true if the value x is null
- Finally, we have printed the result
Turn Learning into Career Growth
JavaScript Program to Check for Null Using Strict Equality Operator
In this approach, we will use the strict equality operator '===' to check if the variable contains null or not. If the value of x is null, then the statement inside the If block will be printed.
JavaScript Implementation
Output
Explanation
- In the first step, we created a variable x and initialized it with a null value.
- Inside the if condition, we are simply checking the value of x is null or not by using the strict equality operator '==='
- The condition will be true if the value x is null
- Finally, we have printed the result
JavaScript Program to Check for Null Using the Object.is() Method
In this approach, we will use the Object.is() method to check the type of an object. The Object.is() method accepts two parameters as an argument, the first is the object name, and the second is the object type that we want to check for the object. The Object.is() will return true if the data type of the first parameter will be equal to the second parameter passed as an argument inside the Object.is() method.
JavaScript Implementation
Output
Explanation
- In the first step, we created a variable x and initialized it with a null value.
- Inside the if condition, we have used the Object.is() operator to check the data type of x
- The condition will be true if the data type of the first parameter will be equal to the second parameter passed as an argument inside the Object.is() method.
- Finally, we have printed the result
Conclusion
In this quick article, we have discussed how to check for null in JavaScript and we can extract the following conclusions.
- Null is a primitive type present in JavaScript which indicates the absence of any object value.
- A variable that has not been assigned a value is of type 'undefined'.
- Null is often used at places where an object can be expected, but no object is relevant.
- The typeof method will return a string named 'object' if the variable passed as the parameter is null.
- The Object.is() method will return true if the type of the passed object (type of the first parameter) is the same as the object type passed as parameter (second parameter).