Sometimes, we want to check whether a variable is defined in Node.js and JavaScript.
In this article, we’ll look at how to check whether a variable is defined in Node.js and JavaScript.
How to check whether a variable is defined in Node.js and JavaScript?
To check whether a variable is defined in Node.js and JavaScript, we can check if it’s not null or undefined.
For instance, we write
if (typeof query !== "undefined" && query !== null) {
doStuff();
}
to check if query
isn’t undefined with typeof query !== "undefined"
.
And we check if query
isn’t null with query !== null
.
Conclusion
To check whether a variable is defined in Node.js and JavaScript, we can check if it’s not null or undefined.