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

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 :  
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 notfunction 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 stringchecking("")// calling the checking function with not an empty stringchecking("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 notfunction 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 stringconsole.log(checking(""))// calling the checking function with not an empty stringconsole.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 notfunction checking(str){ if(str.replace(/\s/g,"") == ""){     console.log("Empty String") } else {     console.log("Not Empty String") }} // calling the checking function with empty stringchecking("  ")// calling the checking function with not an empty stringchecking("Hello Javascript")
Output:
Empty String
Not Empty String
Satyabrata_Jena
JavaScript-Misc
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Remove elements from a JavaScript Array
Differences between Functional Components and Class Components in React
Roadmap to Learn JavaScript For Beginners
Difference Between PUT and PATCH Request
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?