Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to check empty/undefined/null string in JavaScript?

  • Last Updated : 13 Aug, 2021

If we want to check any string either it is filled or empty we can use Javascript for such purpose for example : form validation. 
Example : 
 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

html




<!DOCTYPE HTML>
<html>
 
<head>
    <!--function to check that name can not be empty-->
    <script>
        function checkempty(form) {
            if (form.name.value == null ||
                form.name.value == undefined ||
                form.name.value.length == 0) {
               
                alert("Name cannot be empty\n");
                return false;
            } else {
                alert("Your response has been recorded\n");
                return true;
            }
        }
    </script>
</head>
 
<body>
    <div>
        <center>
            <form onsubmit="return checkempty(form1)"
                  action="#" method="post"
                  name="form1">
                <label>Enter your name</label>
                <br>
                <input type="string"
                       placeholder="Enter name"
                       name="name" />
                <br>
                <br>
                <label>Enter Email</label>
                <br>
                <input type=""
                       placeholder="Enter email"
                       name="email" />
                <br>
                <br>
                <label>Enter password</label>
                <br>
                <input type="password"
                       placeholder="Enter password "
                       name="password1" />
                <br>
                <br>
                <label>Confirm password</label>
                <br>
                <input type="password"
                       placeholder="Enter password "
                       name="password2" />
                <br>
                <br>
                <input type="submit"
                       value="submit" />
                <br>
            </form>
        </center>
    </div>
</body>
 
</html>

Output: 
If the name is empty: 
 

After submitting the form: 
 



Simplified with 3 Different Methods:

Method-1 : Using === operator

Using === operator we will check the string is empty or not. If empty then it will return “Empty String” and if the string is not empty it will return “Not Empty String”

Javascript




// function to check string is empty or not
function checking(str)
{
// checking the string using === operator
   if (str === "")
   {
       console.log("Empty String")
       }
       else{
       console.log("Not Empty String")
       }
   }
 
// calling the checking function with empty string
checking("")
// calling the checking function with not an empty string
checking("GeeksforGeeks")

Output:

Empty String

Not Empty String

Method-2 : By using length and ! operator



This function will check length of string also by using ! operator we will check string is empty or not.

Javascript




// function to check string is empty or not
function checking(str)
{
  // checking the string using ! operator and length
  // will return true if empty string and false if string is not empty
    return (!str || str.length === 0 );
}
// calling the checking function with empty string
console.log(checking(""))
// calling the checking function with not an empty string
console.log(checking("GeeksforGeeks"))

Output:

true

false

Method-3 : By using replace

It will ensure that that the string is not just a group of empty spaces where we are doing replace on the spaces.

Javascript




// function to check string is empty or not
function checking(str)
{
 if(str.replace(/\s/g,"") == ""){
     console.log("Empty String")
 }
 else
 {
     console.log("Not Empty String")
 }
}
 
// calling the checking function with empty string
checking("  ")
// calling the checking function with not an empty string
checking("Hello Javascript")

Output:

Empty String

Not Empty String




My Personal Notes arrow_drop_up