• How to check for an empty string in JavaScript
[pastacode lang=”javascript” manual=”javascript%20null%20is-empty%0A%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • If we need to check whether there’s any value in string,
[pastacode lang=”javascript” manual=”if%20(strValue)%20%7B%0A%20%20%20%20%2F%2Fdo%20something%0A%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]
  • If we need to check specifically for an empty string over null, we would think checking against “” is our best, using the === operator (In fact, a string we are comparing against).

[pastacode lang=”javascript” manual=”if%20(!a)%20%7B%0A%20%20%2F%2F%20is%20emtpy%0A%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]
  • To ignore white space for strings:
[pastacode lang=”javascript” manual=”if%20(!a.trim())%20%7B%0A%20%20%20%20%2F%2F%20is%20empty%20or%20whitespace%0A%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/] [ad type=”banner”]
  • If we need legacy support (IE8-) for trim(), use $.trim or a polyfill.

  • check if the variable has a truthy value or not. That means
[pastacode lang=”javascript” manual=”if(%20value%20)%20%7B%0A%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

will evaluate to true if value is not:

  • null
  • undefined
  • NaN
  • empty string (“”)
  • 0
  • false

The above list represents all possible false values in ECMA-/Javascript. Find it in the specification at the ToBoolean section.
Furthermore, if we do not know whether a variable exists (that means, if it was declared) then we should check with the typeof operator. For instance

[pastacode lang=”javascript” manual=”if(%20typeof%20wiki!%3D%3D%20’undefined’%20)%20%7B%0A%20%20%20%20%2F%2F%20wiki%20could%20get%20resolved%20and%20it’s%20defined%0A%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]
  • Make sure that a variable is declared at least, we should directly check if it has a truthy value

[pastacode lang=”javascript” manual=”function%20isEmpty(str)%20%7B%0A%20%20%20%20return%20(!str%20%7C%7C%200%20%3D%3D%3D%20str.length)%3B%0A%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]
  • If a string is blank, null or undefined then we can use the following:
[pastacode lang=”javascript” manual=”function%20isBlank(str)%20%7B%0A%20%20%20%20return%20(!str%20%7C%7C%20%2F%5E%5Cs*%24%2F.test(str))%3B%0A%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]
  • If a string is blank or contains only white-space:
[pastacode lang=”javascript” manual=”String.prototype.isEmpty%20%3D%20function()%20%7B%0A%20%20%20%20return%20(this.length%20%3D%3D%3D%200%20%7C%7C%20!this.trim())%3B%0A%7D%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • Using !!(not not) operator:
[pastacode lang=”javascript” manual=”if(!!str)%7B%0Asome%20code%20here%3B%0A%7D” message=”jQuery Code” highlight=”” provider=”manual”/]

or

Using type casting:

[pastacode lang=”javascript” manual=”if(Boolean(str))%7B%0A%20%20%20%20codes%20here%3B%0A%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/] [ad type=”banner”]
  • Both do the same function, type cast the variable to boolean, where str is a variable.
  • Returns false for null,undefined,0,000,””,false.
  • Returns true for string “0” and whitespace ” “.

  • Make sure that the string is not just a bunch of empty spaces (we are assuming this is for form validation) we need to replace on the spaces.
[pastacode lang=”javascript” manual=”if(str.replace(%2F%5Cs%2Fg%2C%22%22)%20%3D%3D%20%22%22)%7B%0A%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • We can use the following Solution:
[pastacode lang=”javascript” manual=”function%20empty(e)%20%7B%0A%20%20switch%20(e)%20%7B%0A%20%20%20%20case%20%22%22%3A%0A%20%20%20%20case%200%3A%0A%20%20%20%20case%20%220%22%3A%0A%20%20%20%20case%20null%3A%0A%20%20%20%20case%20false%3A%0A%20%20%20%20case%20typeof%20this%20%3D%3D%20%22undefined%22%3A%0A%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20default%3A%0A%20%20%20%20%20%20return%20false%3B%0A%20%20%7D%0A%7D%0A%0Aempty(null)%20%2F%2F%20true%0Aempty(0)%20%2F%2F%20true%0Aempty(7)%20%2F%2F%20false%0Aempty(%22%22)%20%2F%2F%20true%0Aempty((function()%20%7B%0A%20%20%20%20return%20%22%22%0A%20%20%7D))%20%2F%2F%20true%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • Several methods:
[pastacode lang=”javascript” manual=”%2F%2Fwhen%20undefined%0Aif%20(typeof%20MyVariable%20%3D%3D%20’undefined’)%0A%0A%2F%2Fwhen%20false%0Aif%20(MyVariable%20%3D%3D%20false)%20%2F%2Fsame%20as%20if(!MyVariable%20)%0A%0A%2F%2Fwhen%20defined%2C%20but%20empty%0Aif%20(%0A%20%20%20%20(MyVariable.length%20%3D%3D%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%7C%0A%20%20%20%20(MyVariable%20%3D%3D%20%22%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%7C%0A%20%20%20%20(MyVariable.replace(%2F%5Cs%2Fg%2C%22%22)%20%3D%3D%20%22%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%7C%0A%20%20%20%20(!%2F%5B%5E%5Cs%5D%2F.test(MyVariable))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%7C%0A%20%20%20%20(%2F%5E%5Cs*%24%2F.test(MyVariable))%0A%20%20%20)%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • Here we can use the regexps:
[pastacode lang=”javascript” manual=”if((%2F%5E%5Cs*%24%2F).test(str))%20%7B%20%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/] [ad type=”banner”]
  • Checks for strings that are either empty or filled with whitespace.

  • For example, if we have a null character string:
[pastacode lang=”javascript” manual=”var%20y%20%3D%20%22%5C0%22%3B%20%2F%2F%20an%20empty%20string%2C%20but%20has%20a%20null%20character%0A(y%20%3D%3D%3D%20%22%22)%20%2F%2F%20false%2C%20testing%20against%20an%20empty%20string%20does%20not%20work%0A(y.length%20%3D%3D%3D%200)%20%2F%2F%20false%0A(y)%20%2F%2F%20true%2C%20this%20is%20also%20not%20expected%0A(y.match(%2F%5E%5B%5Cs%5D*%24%2F))%20%2F%2F%20false%2C%20again%20not%20wanted%0A” message=”jQuery Code” highlight=”” provider=”manual”/]
  • To test its nullness, we can do the following:
[pastacode lang=”javascript” manual=”String.prototype.isNull%20%3D%20function()%7B%20%0A%20%20return%20Boolean(this.match(%2F%5E%5B%5C0%5D*%24%2F))%3B%20%0A%7D%0A…%0A%22%5C0%22.isNull()%20%2F%2F%20true%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

It works on a null string, and on an empty string and it is accessible for all strings. In addition, it could be expanded to contain other JavaScript empty or whitespace characters (i.e. nonbreaking space, byte order mark, line/paragraph separator, etc.).

[pastacode lang=”javascript” manual=”%09%0A%0Afunction%20isBlank(pString)%7B%0A%20%20%20%20if%20(!pString%20%7C%7C%20pString.length%20%3D%3D%200)%20%7B%0A%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%7D%0A%20%20%20%20%2F%2F%20checks%20for%20a%20non-white%20space%20character%20%0A%20%20%20%20%2F%2F%20which%20I%20think%20%5Bcitation%20needed%5D%20is%20faster%20%0A%20%20%20%20%2F%2F%20than%20removing%20all%20the%20whitespace%20and%20checking%20%0A%20%20%20%20%2F%2F%20against%20an%20empty%20string%0A%20%20%20%20return%20!%2F%5B%5E%5Cs%5D%2B%2F.test(pString)%3B%0A%7D%0A” message=”jQuery Code” highlight=”” provider=”manual”/]

  • Starting with:
[pastacode lang=”javascript” manual=”return%20(!value%20%7C%7C%20value%20%3D%3D%20undefined%20%7C%7C%20value%20%3D%3D%20%22%22%20%7C%7C%20value.length%20%3D%3D%200)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]
  • Looking at the last condition, if value == “”, it’s length MUST be 0. Therefore drop it:
[pastacode lang=”javascript” manual=”return%20(!value%20%7C%7C%20value%20%3D%3D%20undefined%20%7C%7C%20value%20%3D%3D%20%22%22)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]
  • In JS, an empty string is false. Therefore, drop value == “”:
[pastacode lang=”javascript” manual=”return%20(!value%20%7C%7C%20value%20%3D%3D%20undefined)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]
  • And !undefined is true, so that check isn’t needed. So we have:
[pastacode lang=”javascript” manual=”return%20(!value)%3B%0A” message=”jQuery Code” highlight=”” provider=”manual”/]
  • And we don’t need parentheses:
[pastacode lang=”javascript” manual=”return%20!value%0A” message=”jQuery Code” highlight=”” provider=”manual”/] [ad type=”banner”]

Categorized in: