How to Check if a Variable is Not null in JavaScript
In JavaScript, null is a primitive value that represents the intentional absence of any object value. A common task in programming is to check if a variable is not null before you try to access its properties or use it in an operation. This is a crucial validation step to prevent TypeError exceptions in your code.
This guide will teach you the definitive method for checking if a variable is not null using the strict inequality operator (!==). You will also learn the important distinction between checking only for null and checking for both null and undefined.
The Core Method (Recommended): The Strict Inequality Operator (!==)
The simplest, safest, and most explicit way to check if a variable's value is not null is with the strict inequality operator (!==).
The syntax:
myVariable !== null
!==: This operator checks if the two operands are not equal and are not of the same type. It does not perform any type coercion.
This check will return true for every value except for the primitive null value itself.
Basic Example: A Simple null Check
This script demonstrates how to use !== to safely handle a variable that might be null.
function processValue(value) {
if (value !== null) {
console.log(`The value is not null. It is: '${value}'`);
} else {
console.log('The value is null. No action taken.');
}
}
processValue('hello'); // Output: The value is not null. It is: 'hello'
processValue(0); // Output: The value is not null. It is: '0'
processValue(undefined); // Output: The value is not null. It is: 'undefined'
processValue(null); // Output: The value is null. No action taken.
A Common Related Task: Checking for null or undefined
Often, you don't just want to check for null; you want to check if a variable is "missing" or has "no value," which in JavaScript usually means checking for both null and undefined. These two values are often called "nullish" values.
There are three main ways to perform this check.
Method A (Explicit and Recommended): !== null && !== undefined
This is the most explicit and readable method.
const myVar = 'hello';
if (myVar !== null && myVar !== undefined) {
console.log('The variable is not nullish.');
}
Method B (Loose Inequality): != null
This is a clever and common shortcut. The loose inequality operator (!=) does perform type coercion, and it is specifically designed to treat null and undefined as equal. Therefore, myVar != null is a concise way to check if myVar is not null and not undefined.
const myVar = undefined;
if (myVar != null) {
// This block will NOT run
} else {
console.log('The variable is either null or undefined.');
}
Method C (Modern): The Nullish Coalescing Operator (??)
If your goal is to provide a default value for a nullish variable, the ?? operator is the best tool.
const myVar = null;
const myDefault = 'default string';
const result = myVar ?? myDefault;
console.log(result); // Output: default string
How the Comparison Operators Work
- Strict Inequality (
!==): This operator is the safest for checkingnull. It does not try to convert types.'hello' !== null->true(different values, different types)0 !== null->true(different values, different types)undefined !== null->true(different values)null !== null->false
- Loose Inequality (
!=): This operator is less strict.'hello' != null->true0 != null->trueundefined != null->false(becauseundefined == nullistrue)
Conclusion
Checking if a variable is not null is a fundamental validation step in JavaScript.
The key takeaways are:
- To check if a variable is specifically not
null, the most robust and readable method is the strict inequality operator:if (myVar !== null). - If you need to check if a variable is neither
nullnorundefined(is not "nullish"), the concise shortcut is to use the loose inequality operator:if (myVar != null). - Avoid simple "truthiness" checks (
if (myVar)), as this will incorrectly reject valid "falsy" values like0or an empty string"".
By choosing the correct operator for your specific needs, you can write clear, predictable, and bug-free conditional logic.