In this article, you’ll learn how to check if a Variable is not Null by directly checking using strict equality or checking for falsy values.
Check if a Variable is not Null in JavaScript
Checking for Falsy values
An interesting way. Though it has some problems which will be explained in a bit.
Two values can technically be equal if they’re both considered falsy or truthy, though only if you’re using ==
or !=
(regular equality).
So you can put the variable into an if statement to check if null
like this:
Code:
const val = null;
if (val) console.log("Not null");
else console.log("Is null");
Output:
"Is null"
Though an issue with this is that some values like undefined or ""
(empty string) are also falsy values, so we get something like this:
Code:
const val = "";
if (val) console.log("Not null");
else console.log("Is null");
Output:
"Is null"
You can view the list of falsy values here.
Checking Directly using !==
or ===
The most direct way. simply compare the variable to null
.
Code:
const val = null;
if (val === null) console.log("Is null");
else console.log("Not null");
Output:
"Is null"
Though please note that it is required that you use ===
or !==
(strict equality) over ==
and !=
(equality) respectively, since ===
and !==
additionally checks if both values are the same type. The big reason for this is that two values could still be equal if they’re considered falsy, as explained above.
Using Nullish Related Statements
The more intermediate way to do it. If you feel intimidated by this chapter, you can read it another time.
While some of it won’t directly tell you if the variable is null
or not, it does check for null
and has applications elsewhere.
Nullish Coalescing Operator (??)
Syntax: x ?? y
The Nullish Coalesching Operator is a logic operator (similar to "?"
) that picks between x
and y
where x
is chosen if it’s not null
or undefined
and y
if it is.
Additionally, it can also perform a single line of code (which can be a function call) if x
is not null
or undefined
.
Code:
const val1 = "LearnShareIT";
const val2 = null;
const val_final_1 = val1 ?? "Is null";
const val_final_2 = val2 ?? "Is null";
console.log(val_final_1);
console.log(val_final_2);
Output:
"LearnShareIT"
"Is null"
Or it can be shortened as:
Code:
const val1 = "LearnShareIT";
const val2 = null;
val1 ?? (console.log("Is null"));
val2 ?? (console.log("Is null"));
Output:
"Is null"
Logical Nullish Assignment (??=
)
Syntax: x ??= y
The Logical Nullish Assignment is an assignment operator (similar to "="
) that assigns y
to x
if x is null
or undefined
.
Code:
let val_final_1 = "LearnShareIT";
let val_final_2 = null;
val_final_1??= "Is null";
val_final_2??= "Is null";
console.log(val_final_1);
console.log(val_final_2);
Output:
"LearnShareIT"
"Is null"
An interesting fact: The nullish assignment is equivalent to x ?? (x = y)
Optional Chaining (?.)
Syntax: x?.y
The Optional Chaining Operator accesses (similar to "."
) a property or function if the Object containing it is not null
or undefined
, otherwise it returns undefined
.
Code:
const obj = {
x: "Not null"
}
console.log(obj?.x);
console.log(obj?.y);
Output:
"Not null"
undefined
Summary
To check if a Variable is not Null in JavaScript, you can check directly using strict equality (!==
or ===
) or simply checking for falsy values though this is not suggested. You can read more information on the difference between strict and regular equality here.
Maybe you are interested:
- If variable is Null or Undefined in JavaScript
- Check if Variable is of Function Type using JavaScript
- Check if a Variable is of type Error in JavaScript
- Check if a variable is a string using javascript

Hello, my name is Davis Cole. I love learning and sharing knowledge about programming languages. Some of my strengths are Java, HTML, CSS, JavaScript, C#,… I believe my articles about them will help you a lot.
Programming Languages: Java, HTML, CSS, JavaScript, C#, ASP.NET, SQL, PHP