Example 1: Check undefined or null
// program to check if a variable is undefined or null
function checkVariable(variable) {
if(variable == null) {
console.log('The variable is undefined or null');
}
else {
console.log('The variable is neither undefined nor null');
}
}
let newVariable;
checkVariable(5);
checkVariable('hello');
checkVariable(null);
checkVariable(newVariable);
Output
The variable is neither undefined nor null The variable is neither undefined nor null The variable is undefined or null The variable is undefined or null
In the above program, a variable is checked if it is equivalent to null
. The null
with ==
checks for both null
and undefined
values. This is because null == undefined
evaluates to true.
The following code:
if(variable == null) { ... }
is equivalent to
if (variable === undefined || variable === null) { ... }
Example 2: using typeof
// program to check if a variable is undefined or null
function checkVariable(variable) {
if( typeof variable === 'undefined' || variable === null ) {
console.log('The variable is undefined or null');
}
else {
console.log('The variable is neither undefined nor null');
}
}
let newVariable;
checkVariable(5);
checkVariable('hello');
checkVariable(null);
checkVariable(newVariable);
Output
The variable is neither undefined nor null The variable is neither undefined nor null The variable is undefined or null The variable is undefined or null
The typeof
operator for undefined
value returns undefined. Hence, you can check the undefined
value using typeof
operator. Also, null
values are checked using the ===
operator.
Note: We cannot use the typeof
operator for null
as it returns object.
Also Read: