OneBite.Dev - Coding blog in a bite size

check if a string is empty in Javascript

Code snippet on how to check if a string is empty in Javascript

  if (string === '') {
    console.log('The string is empty.')
  }

This code will check if a given string is empty in javascript. First, it uses the ‘if’ statement to check if the variable ‘string’ is equal to an empty string, indicated with the two single quotation marks surrounded by double equals. If the variable is equal to an empty string, then the ‘console.log’ command will print a string saying ‘The string is empty.’ to the console. This code provides a simple, efficient way to check for an empty string in Javascript.

javascript