How to check if a String is Empty in JavaScript

Check if a String is Empty in JavaScript

To check if a string is empty:

  1. Use the length property.
  2. Check if string’s length is equal to zero.
  3. If the string’s length is equal to zero , then it’s empty, otherwise it isn’t empty.

Example –

const theString = '';

if (typeof theString === 'string' && theString.length === 0) {
  console.log('It's an empty string');
} else {
  console.log('It's not an empty string')
}
Check if a String is Empty in JavaScript using length property.
Check if a String is Empty in JavaScript using length property.

You can use the trim() method to check if a string is empty by removing any leading or trailing whitespace.
Example –

const theString = '   ';

if (typeof theString === 'string' && theString.trim().length === 0) {
  console.log('It's an empty string');
} else {
  console.log('It's not an empty string')
}
Trim white spaces before checking if string is empty.
Trim white spaces before checking if string is empty.

Note – The trim() method removes any whitespace characters from the beginning and end of a string, and returns a new string. This includes space characters, tab characters, and all other whitespace characters. It also includes all line terminator characters (LF, CR, etc.).

If you want to check if a string is truthy and contains at least one character, you can pass it to an if statement.

const theString = 'Hello';

if (theString) {
  console.log("string is truthy")
}
Check if a string is truthy.
Check if a string is truthy.

If the string variable is set to an empty string, undefined, null, 0, false or NaN, the code in the if block will not run.

You can also check that the value is not an empty string.

Example –

const theString = '';

if (typeof theString === 'string' && theString !== '') {
  console.log("String is not empty")
}
Check that the value is not equal to an empty string.
Check that the value is not equal to an empty string.

In this example, if string is NOT empty the if code block will run.

We used the && operator to specify that both conditions have to be true in order for the if block to run.

This approach wouldn’t work for a string that contains an empty space ” “. If you need to handle strings that contain only spaces, use the trim() method.

Example –

const theString = '     ';

if (typeof theString === 'string' && theString.trim() !== '') {
  console.log("String is not empty")
}
Trim white spaces and check if string is empty.
Trim white spaces and check if string is empty.

The trim() method returns a new string without the whitespace from both ends of the original string.

const theString = '  Hello   ';

const trimmed  = theString.trim();
console.log(trimmed);
Trim white spaces.
Trim white spaces.

If you’re taking user input and want to make sure the user didn’t just enter a whitespace character to bypass your validation, this is very useful.

To check if a string is empty, we can access its length property. If the string’s length is 0, then it is empty.

const theString = 'Hello';

if (typeof theString === 'string' && theString.length !== 0) {
  console.log("theString is NOT empty")
}
Access length property to check if string is empty.
Access length property to check if string is empty.
🗒️Note - Make sure that the variable is set to a string before trying to access its length property, as trying to access the length property on a variable that is undefined or null will result in an error.