Check If String Is Empty in JavaScript

Find out the two best ways to check whether a string is empty or not in JavaScript.

Published Categorized as JavaScript, Web Dev

How can you check if a given string is empty in JavaScript?

There are two things you need to know before we get to the answer:

  • In JavaScript, an empty string is also known as a falsy. A “falsy” is any value that, in a boolean, would equate to false. A null, an undefined, and an empty string are all examples of falsies.
  • The opposite of a falsy is, you guessed it, a truthy. A “truthy” is any value that, in a boolean, would equate to true. As a general rule of thumb, all values are truthies unless they are defined as a falsies.

Remember these two definitions as you read on. Not only will they help you understand how this type of check works, but that understanding will help you build better applications.

Checking for an Empty String

The two fastest, most computationally efficient ways to check for an empty string in JavaScript are to test the string’s length property or to use the strict equality operator.

In both of these solutions, the interpreter doesn’t have to turn the string literal into a String object to perform the test.

Test the String’s Length Property

My go-to way is to test the string’s length property.

Here’s how this test looks like:

if (myString.length == 0) {
// Do this when the string is empty
}

For an empty string, length is 0, which means the string is a falsy.

You could, of course, extend this conditional statement by adding an “else” for when the string isn’t empty (and is therefore a truthy):

if (myString.length == 0) {
// Do this when the string is empty
}
else {
// Do this when the string is not empty
}

The same test can be done in reverse order, by checking whether the length property of the given string is higher than 0:

if (myString.length > 0) {
// Do this when the string is not empty
}
else {
// Do this when the string is empty
}

Test for an Empty String Literal

The second way to check for an empty string is to test for an empty string literal:

if (myString === "") {
// Do this when the string literal is empty
}

Once again, you’re testing for a falsy. And extended, with an “else” for when the test results in a truthy:

if (myString === "") {
// Do this when the string literal is empty
}
else {
// Do this when the string literal is not empty
}

Notice that, in the code snippet above, we’re using the “===” strict equality operator instead of less strict counterpart, the “==” equality operator.

The equality operator (“==”) does type coercion, whereas the strict equality operator (“===”) doesn’t. For reasons explained well by others in this Stackoverflow thread, using the former can cause unexpected behavior of your application and make it less performant.

Which Solution to Use

Whether you test the string’s length property for equality to 0 or strictly equate the string lateral to “” comes down to the context of your application and your implementation approach.

That said, testing the string’s length will throw an error when the string itself is undefined, whereas strictly equating its lateral to “” won’t.

If there is a scenario in which your string is undefined, go for the second solution over the first one.

Image courtesy of eskaylim /Depositphotos

Leave a comment

Your email address will not be published.