Empty Strings

By: Dusty Arlia
Published on September 6, 2012
Last Updated on Wednesday, July 08, 2015 at 8:41 PM
Total Updates: 6

An empty string can be two single quotation marks ('') or two double quotation marks (""). They can be useful when used in methods like the prompt() method. When used as a second argument in the prompt() method, the results are a blank text field in the prompt box. Look at this example:

prompt("What is your name?", "");

In this example, the second argument is an empty string.

If you need to test a variable to make sure it is or isn't an empty string, compare it to an empty string ("" or '') using the Identity Operator (===). Here is an example:

var am_i_an_empty_string = " ";      //notice the space
if (am_i_an_empty_string === "") {
    alert("I am an empty string");
} else {
    alert("I am not an empty string");

The message I am not an empty string gets displayed in an alert box. This is because the variable am_i_an_empty_string is not assigned a value of an empty string (it is assigned a space).