• How to check for an empty string in JavaScript
jQuery Code
javascript null is-empty

  • If we need to check whether there’s any value in string,
jQuery Code
if (strValue) {
//do something
}
  • 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).

jQuery Code
if (!a) {
// is emtpy
}
  • To ignore white space for strings:
jQuery Code
if (!a.trim()) {
// is empty or whitespace
}
[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
jQuery Code
if( value ) {
}

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

jQuery Code
if( typeof wiki!== 'undefined' ) {
// wiki could get resolved and it's defined
}
  • Make sure that a variable is declared at least, we should directly check if it has a truthy value

jQuery Code
function isEmpty(str) {
return (!str || 0 === str.length);
}
  • If a string is blank, null or undefined then we can use the following:
jQuery Code
function isBlank(str) {
return (!str || /^\s*$/.test(str));
}
  • If a string is blank or contains only white-space:
jQuery Code
String.prototype.isEmpty = function() {
return (this.length === 0 || !this.trim());
};

  • Using !!(not not) operator:
jQuery Code
if(!!str){
some code here;
}

or

Using type casting:

jQuery Code
if(Boolean(str)){
codes here;
}
[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.
jQuery Code
if(str.replace(/\s/g,"") == ""){
}

  • We can use the following Solution:
jQuery Code
function empty(e) {
switch (e) {
case "":
case 0:
case "0":
case null:
case false:
case typeof this == "undefined":
return true;
default:
return false;
}
}

empty(null) // true
empty(0) // true
empty(7) // false
empty("") // true
empty((function() {
return ""
})) // true

  • Several methods:
jQuery Code
//when undefined
if (typeof MyVariable == 'undefined')

//when false
if (MyVariable == false) //same as if(!MyVariable )

//when defined, but empty
if (
(MyVariable.length == 0)
||
(MyVariable == "")
||
(MyVariable.replace(/\s/g,"") == "")
||
(!/[^\s]/.test(MyVariable))
||
(/^\s*$/.test(MyVariable))
)

  • Here we can use the regexps:
jQuery Code
if((/^\s*$/).test(str)) { }
[ad type=”banner”]
  • Checks for strings that are either empty or filled with whitespace.

  • For example, if we have a null character string:
jQuery Code
var y = "\0"; // an empty string, but has a null character
(y === "") // false, testing against an empty string does not work
(y.length === 0) // false
(y) // true, this is also not expected
(y.match(/^[\s]*$/)) // false, again not wanted
  • To test its nullness, we can do the following:
jQuery Code
String.prototype.isNull = function(){ 
return Boolean(this.match(/^[\0]*$/));
}
...
"\0".isNull() // true

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.).

jQuery Code
	

function isBlank(pString){
if (!pString || pString.length == 0) {
return true;
}
// checks for a non-white space character
// which I think [citation needed] is faster
// than removing all the whitespace and checking
// against an empty string
return !/[^\s]+/.test(pString);
}

  • Starting with:
jQuery Code
return (!value || value == undefined || value == "" || value.length == 0);
  • Looking at the last condition, if value == “”, it’s length MUST be 0. Therefore drop it:
jQuery Code
return (!value || value == undefined || value == "");
  • In JS, an empty string is false. Therefore, drop value == “”:
jQuery Code
return (!value || value == undefined);
  • And !undefined is true, so that check isn’t needed. So we have:
jQuery Code
return (!value);
  • And we don’t need parentheses:
jQuery Code
return !value
[ad type=”banner”]

Categorized in: