How to Check if a Variable Is Not Null in JavaScript
- Using the Strict Equality Operator (===)
- Using the Loose Equality Operator (==)
- Using the Logical NOT Operator (!)
- Using the typeof Operator
- Conclusion
- FAQ

In JavaScript, ensuring that a variable is not null is a fundamental aspect of writing robust and error-free code. Whether you’re dealing with user inputs, API responses, or any dynamic data, verifying the presence of a value can prevent unexpected behaviors and bugs.
This tutorial will guide you through various methods to check if a variable is not null in JavaScript. By the end, you’ll have a solid understanding of how to implement these checks effectively, ensuring your code runs smoothly. Let’s dive in and explore the different ways to verify the integrity of your variables!
Using the Strict Equality Operator (===)
One of the most straightforward methods to check if a variable is not null is by using the strict equality operator (===). This operator compares both the value and the type of the variable, making it a reliable choice for type-sensitive comparisons.
Here’s how you can implement this method:
let myVar = "Hello, World!";
if (myVar !== null) {
console.log("The variable is not null.");
} else {
console.log("The variable is null.");
}
Output:
The variable is not null.
In the code above, we declare a variable called myVar
and assign it a string value. The if statement checks if myVar
is strictly not equal to null. If it’s not null, it logs a message confirming that the variable holds a valid value. This method is simple and effective, especially when you want to ensure that your variable is not only defined but also holds a meaningful value.
Using the Loose Equality Operator (==)
Another method to check if a variable is not null is by using the loose equality operator (==). Unlike the strict equality operator, this one performs type coercion, which means it can compare different types. While this can be useful in some cases, it’s important to be cautious when using it due to potential unexpected results.
Here’s an example:
let myVar = null;
if (myVar != null) {
console.log("The variable is not null.");
} else {
console.log("The variable is null.");
}
Output:
The variable is null.
In this example, we declare myVar
and set it to null. The loose equality operator checks if myVar
is not equal to null. Since it is null, the else block executes, informing us that the variable is indeed null. While this method is less strict, it can be handy when you want to check for both null and undefined values at the same time.
Using the Logical NOT Operator (!)
The logical NOT operator can be a powerful tool when checking for null values. By negating the variable, you can create a condition that checks if the variable is truthy, which includes checking for null, undefined, and other falsy values.
Here’s how you can use it:
let myVar = "JavaScript";
if (!myVar) {
console.log("The variable is null or undefined.");
} else {
console.log("The variable is not null.");
}
Output:
The variable is not null.
In this code, we assign a string to myVar
. The if statement uses the logical NOT operator to check if myVar
is falsy. Since it contains a valid string, the else block executes, confirming that the variable is not null. This method is concise and works well for quick checks, but keep in mind that it will also evaluate other falsy values like 0 and empty strings.
Using the typeof Operator
Another effective way to check if a variable is not null is by using the typeof
operator. This operator returns a string indicating the type of the unevaluated operand, allowing you to specifically check for the null type.
Here’s an example:
let myVar;
if (typeof myVar !== 'object' || myVar !== null) {
console.log("The variable is not null.");
} else {
console.log("The variable is null.");
}
Output:
The variable is null.
In this example, we declare myVar
without assigning it a value, making it undefined. The if statement checks the type of myVar
. Since it is undefined, the condition evaluates to true, and we see that the variable is not null. This method is particularly useful when you want to differentiate between null and other falsy values.
Conclusion
Checking if a variable is not null in JavaScript is crucial for writing reliable code. By utilizing methods such as the strict equality operator, loose equality operator, logical NOT operator, and the typeof operator, you can ensure that your variables hold meaningful values. Each method has its own pros and cons, so it’s essential to choose the one that best fits your specific use case. As you continue to practice and implement these techniques, you’ll find that your code becomes more robust and less prone to unexpected errors.
FAQ
-
What is the difference between null and undefined in JavaScript?
Null is an intentional absence of any value, while undefined means a variable has been declared but has not yet been assigned a value. -
Can I check for both null and undefined at the same time?
Yes, using the loose equality operator (==) can help you check for both null and undefined in a single comparison. -
Is using the strict equality operator recommended?
Yes, using the strict equality operator (===) is generally recommended as it avoids type coercion and provides more predictable results. -
What will happen if I use the logical NOT operator on a non-null variable?
If you use the logical NOT operator on a non-null variable, it will evaluate to false, allowing you to confirm that the variable holds a valid value. -
Are there any performance differences between these methods?
Generally, the performance differences are negligible for most applications, so it’s best to choose the method that makes your code clearer and easier to understand.
Harshit Jindal has done his Bachelors in Computer Science Engineering(2021) from DTU. He has always been a problem solver and now turned that into his profession. Currently working at M365 Cloud Security team(Torus) on Cloud Security Services and Datacenter Buildout Automation.
LinkedInRelated Article - JavaScript Variable
- How to Access the Session Variable in JavaScript
- How to Check Undefined and Null Variable in JavaScript
- How to Mask Variable Value in JavaScript
- Why Global Variables Give Undefined Values in JavaScript
- How to Declare Multiple Variables in a Single Line in JavaScript
- How to Declare Multiple Variables in JavaScript