Categories
JavaScript Answers

How to check something is empty in JavaScript?

Spread the love

Sometimes, we want to check something is empty in JavaScript.

In this article, we’ll look at how to check something is empty in JavaScript.

How to check something is empty in JavaScript?

To check something is empty in JavaScript, we can check the variable for null, undefined, or an empty string.

For instance, we write

if (myVar === "") {
  // ...
}

to check if myVar is an empty string.

We write

if (myVar === null) {
  // ...
}

to check if myVar is null.

And we write

if (myVar === undefined) {
  // ...
}

to check if myVar is undefined.

Conclusion

To check something is empty in JavaScript, we can check the variable for null, undefined, or an empty string.

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 *