. Advertisement .
..3..
. Advertisement .
..4..
The values undefined or null always appear in the code flow. Whether a reference was lost as a side effect, unassigned to an in-memory object, or an empty response was given from other resources, APIs, or databases, checking whether the variable is undefined or null is an operation that usually takes place on Javascript.
In this blog, we will figure out how to check if a variable is undefined or null in Javascript.
Distinguish Undefined and Null Values in Javascript
Though undefined and null often go along with each other, we need to make clear that there is a difference between these two values.
- An undefined variable refers to that something does not exist.
- A null variable is a defined variable that refers to missing values.
Check if Variable is Undefined or Null in Javascript
To determine whether a variable is undefined or null in Javascript, use the operator ||
(or) to access if there is any condition met. When being used with 2 boolean values, the return value is true if one of the conditions is evaluated to be true.
const age = null;
// Check if undefined or null
if (age === undefined || age === null) {
console.log('variable is undefined or null');
} else {
console.log('variable is NOT undefined or null');
}
// Check if NOT undefined or null
if (age !== undefined && age !== null) {
console.log('variable is NOT undefined or null');
}
Firstly, we use the if statement to access whether the age variable is undefined or null. Then, the block if will run when one of the 2 conditions is met, otherwise the block else will run.
We then use the if statement again to assess whether the variable is different from null and different from undefined. For this case, the logical operator && (and) is used for specifying that those 2 conditions must be responded to for the block if to run.
With all the above examples, strict equality === is used. However, you can alternatively use the loose equality == to determine whether a variable is undefined or null.
For example, using the operator of loose equality (==
) to check whether a variable is undefined or null, the return value of age == null is true when the age variable is undefined or null. Utilizing the operator of the loose equality to compare undefined with null also returns true.
const age = undefined;
if (age == null) {
console.log('variable is undefined or null');
}
console.log(null == undefined); // true
Because the loose equality (==
) is used, only when the age variable is null or undefined then the condition can be met. This is quite more obscure than applying the operator of strict equality. Not only that, perhaps this method makes some developers confused to read your code.
Conclusion
With our short guide, you are introduced to how to determine if a variable is undefined or null in JavaScript by using various operators. We also note the strengths and limits of each method. Hopefully, this blog is useful for you to learn and consider which method should be used to perform this functionality. If you would like to explore more IT tutorials, you can stay on our website as long as you want.
Read more
Leave a comment