How to check if a string is empty in JavaScript?

Published February 13, 2021

To check if a string is empty you can use the strict equality operator (or the triple equals ===) and compare it with an empty string (or empty brackets "" ) in JavaScript.

You can do it like this,

// Check if string is empty
if (str === "") {
  // This block of code will be executed
  // if the string is empty
}

You might be tempted to do a simple checking with an if/else conditional statement without using the equality checking like this,

const str = "";

// Check if string is empty
// without using the === operator
if (str) {
  // This block of code will
  // not be executed
}

But here the, if statement will not run since an empty string is a falsy value in JavaScript.

See this example live in JSBin.

Feel free to share if you found this useful 😃.


Share on: Facebook Twitter