Categories
JavaScript Answers

How to check whether a variable is defined in Node.js and JavaScript?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.