This article will see how to check if a string is empty using different methods and various examples.

Table of Contents:-

Javascript check if string is empty using === operator

In the below example, a function is used to determine if the string is empty or not. If blank, the function will print The string is empty else, it will print The string is not empty.

function isEmptyCheck(string1) {
    if (string1 === "") {
        console.log("The string is empty")
        }
        else{
        console.log("The string is not empty") 
        }
    }
isEmptyCheck("") 
isEmptyCheck("Hello Javascript")

Output:-

The string is empty
The string is not empty

Javascript check if string is empty using length and ! operator

In the below example, a function will determine if the string is empty or not. If empty, the function returns true else it will return false.

function isEmptyFunction(_string) {
    return (!_string || _string.length === 0 );
}
console.log(isEmptyFunction(""))
console.log(isEmptyFunction("Hello Javascript"))

Output:-

true
false

Javascript check if string is empty or has only white spaces

In the below example, the function will determine if the string is empty or not or has only white spaces. The function would return true if the string passed as an argument is empty or has only white spaces. The function returns false for other inputs.

function isEmptyFunction2(_string1) {
    return (_string1.length === 0 || !_string1.trim());
};
console.log(isEmptyFunction2("Hello Javascript"))
console.log(isEmptyFunction2(""))
console.log(isEmptyFunction2("    "))

Output:-

false
true
true

Important Note: The function will return an error “trim is not a function” if the input argument passed is not a string.

Javascript check if string is valid using typecasting

In the below example, the function will determine if the string is valid or not. The function will typecast the variable to Boolean. The function prints The string is improper when null, undefined, 0, 000, “”, false are input arguments and The string is proper when a string, “0” and whitespace are input arguments.

function isImproperStringCheck(_stringInput)
{
if (Boolean(_stringInput))
{
console.log("The string is proper") //false for null, undefined, 0, 000, "", false.
}
else{
console.log("The string is improper") //true for string "0" and whitespace " ".
}
}
isImproperStringCheck("")
isImproperStringCheck("  ")
isImproperStringCheck("Hello Javascript")
isImproperStringCheck(0)
isImproperStringCheck("0")

Output:-

The string is improper
The string is proper
The string is proper
The string is improper
The string is proper

Javascript check if string is empty using replace

In the below example, the function will determine if the string is empty or not. The function will print The string is blank in case the string is empty or has only white spaces. Else will print The string is not blank

function isEmptyCheck(_string)
{
if(_string.replace(/\s/g,"") == ""){
    console.log("The string is blank") 
}else{
    console.log("The string is not blank") 
}
}
isEmptyCheck("")
isEmptyCheck("  ")
isEmptyCheck("Hello Javascript")

Output:-

The string is blank
The string is blank
The string is not blank

Important Note: The function will return an error “replace is not a function” if the input argument passed is not a string.

Javascript check if string is empty using case statement

In the below example, the function will check different cases and print String is empty/null/undefined/0 if the input argument is empty, null, undefined, false, or 0 else will print String input is valid.

function check_empty(_string) {
    switch (_string) {
      case "":
      case null:
      case false:
      case 0:
      case typeof(_string) == "undefined":
        return "String is empty/null/undefined/0";
      default:
        return "String input is valid";
    }
  }
  console.log(check_empty(null))
  console.log(check_empty("Hello Javascript")) 
  console.log(check_empty(0)) 
  console.log(check_empty(7) )
  console.log(check_empty(""))

Output:-

String is empty/null/undefined/0
String input is valid
String is empty/null/undefined/0
String input is valid
String is empty/null/undefined/0

Read More:

We hope this article helped you to check if a string is empty in javascript. Good Luck !!!