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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *