Last updated: Mar 2, 2024
Reading timeยท4 min
Use the strict inequality (!==) operator to check if a variable is not null,
e.g. myVar !== null
.
The strict inequality operator will return true
if the variable is not equal
to null
and false
otherwise.
const a = 'hello'; // โ check if a variable is NOT null if (a !== null) { // ๐๏ธ this runs console.log('the variable is NOT null'); } else { console.log('the variable is null'); } // ------------------------------------- // โ check if a variable is NOT null and undefined if (a != null) { // ๐๏ธ this runs console.log('the variable is NOT null and undefined'); } else { console.log('The variable is null or undefined'); } // ------------------------------------- // โ check if a variable has been declared (without errors) if (typeof abcd !== 'undefined') { console.log('The variable has been declared'); } else { // ๐๏ธ this runs console.log('The variable is undeclared or set to undefined'); }
We used the
strict inequality (!==)
operator to check if the value stored in a variable is not equal to null
.
The operator returns a boolean result:
true
if the values are not equalfalse
if the values are equalconsole.log(null !== null); // ๐๏ธ false console.log('hello' !== null); // ๐๏ธ true
The strict inequality (!==) operator considers two values of different types to be different, as opposed to the loose inequality (!=) operator.
This means that if you compare null
with any other type, the strict
inequality (!==) operator will always return false
.
truthy
, instead of checking if it's not null. Here's an example.const a = 'hello'; if (a) { console.log(`๐จ a is NOT false, 0, empty string, null, undefined, NaN`); } else { console.log(`โ๏ธ๏ธ a is ONE OF false, 0, empty string, null, undefined, NaN`); }
In this example, we check if the value stored in the variable a
is truthy.
Truthy are all values that are not falsy.
The falsy values in JavaScript are: false
, 0
, ""
, null
, undefined
,
NaN
.
else
block runs, the value of the variable is falsy and could be either of the 6 falsy values and not necessarily null
.Things could go wrong in many different ways when doing this. For example, if
the value of a
is equal to 0
, the else
block would run.
const a = 0; if (a) { console.log("โ๏ธ This doesn't run"); } else { console.log('โ This runs'); }
Even if this is what you want, it is better to be explicit.
const a = 0; if (a === 0) { console.log('โ This runs'); } else { console.log("โ๏ธ This doesn't run"); }
It's always recommended to explicitly use the strict operators (!==, ===) when making equality comparisons. They make your code more readable, explicit and less prone to errors.
One exception is when you need to check if a variable is not nullish.
Here are 2 examples of checking if a variable is not nullish (null
and
undefined
).
const a = 'hello'; // โ check if a variable is NOT null and undefined if (a != null) { // ๐๏ธ this runs console.log('the variable is NOT null and undefined'); } else { console.log('The variable is null or undefined'); } // ------------------------------------- // โ check if a variable is NOT nullish (null and undefined) if (a !== null && a !== undefined) { // ๐๏ธ this runs console.log('the variable is NOT null and undefined'); } else { console.log('The variable is null or undefined'); }
The first example uses the loose inequality (!=) operator.
const a = 'hello'; if (a != null) { // ๐๏ธ this runs console.log('the variable is NOT null and undefined'); } else { console.log('The variable is null or undefined'); }
The expression a != null
checks if the variable a
is not equal to both
null
and undefined
.
When using the loose equality (==) operator, null
is equal to undefined
.
// ๐๏ธ loose equality console.log(null == undefined); // ๐๏ธ true // ๐๏ธ strict equality console.log(null === undefined); // ๐๏ธ false
An alternative approach to check if a variable is not nullish (null
and
undefined
) is to be explicit and use the logical AND (&&) operator.
const a = 'hello'; if (a !== null && a !== undefined) { // ๐๏ธ this runs console.log('the variable is NOT null and undefined'); } else { console.log('The variable is null or undefined'); }
We used the logical AND (&&) operator, so for the if
block to run, both
conditions have to be met.
The first condition checks if the variable is not equal to null
and the second
checks if the variable is not equal to undefined
.
Use the typeof
operator to check if a variable has been declared.
The typeof
operator won't throw an error even if the variable has not been
declared.
if (typeof abcd !== 'undefined') { console.log('The variable has been declared'); } else { // ๐๏ธ this runs console.log('The variable is undeclared or set to undefined'); }
Notice that the abcd
variable has not been declared, but no error occurs when
running the code sample.
The typeof
operator returns a string that indicates the type of a value.
Once you find that the variable has been declared, you can check if the variable
is not null
.
if (typeof abcd !== 'undefined') { console.log('The variable has been declared'); if (abc !== null) { console.log('The variable is not null'); } else { console.log('The variable is null'); } } else { // ๐๏ธ this runs console.log('The variable is undeclared or set to undefined'); }
Once we enter the if
block, we know that the variable has been declared and we
can safely access it.
The inner if
statement checks if the variable is not null
.
Which approach you pick is a matter of personal preference. I always use the
direct not null
check - if (a !== null) {}
.
In my experience, it's better to be explicit even if it makes your code more verbose.
Getting an error is not necessarily a bad thing because most of the time, it surfaces a bug in your application.