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.