How to check if a variable is null or empty string or all whitespace in JavaScript?

JavascriptJquery

Javascript Problem Overview


I need to check to see if a variable is null or has all empty spaces or is just blank ("").

I have the following, but it is not working:

var addr;
addr = "  ";

if (!addr) {
    // pull error 
}

If I do the following, it works:

if (addr) {

}​

What I need is something like the C# method String.IsNullOrWhiteSpace(value).

Javascript Solutions


Solution 1 - Javascript

A non-jQuery solution that more closely mimics IsNullOrWhiteSpace, but to detect null, empty or all-spaces only:

function isEmptyOrSpaces(str){
    return str === null || str.match(/^ *$/) !== null;
}

...then:

var addr = '  ';

if(isEmptyOrSpaces(addr)){
    // error 
}

*** EDIT *** Please note that op specifically states: > I need to check to see if a var is null or has any empty spaces or for that matter just blank.

So while yes, "white space" encompasses more than null, spaces or blank my answer is intended to answer op's specific question. This is important because op may NOT want to catch things like tabs, for example.

Solution 2 - Javascript

if (addr == null || addr.trim() === ''){
  //...
}

A null comparison will also catch undefined. If you want false to pass too, use !addr. For backwards browser compatibility swap addr.trim() for $.trim(addr).

Solution 3 - Javascript

You can use if(addr && (addr = $.trim(addr)))

This has the advantage of actually removing any outer whitespace from addr instead of just ignoring it when performing the check.

Reference: http://api.jquery.com/jQuery.trim/

Solution 4 - Javascript

Old question, but I think it deservers a simpler answer.

You can simply do:

var addr = "  ";

if (addr && addr.trim()) {

    console.log("I'm not null, nor undefined, nor empty string, nor string composed of whitespace only.");

}

Solution 5 - Javascript

Simplified version of the above: (from here: https://stackoverflow.com/a/32800728/47226)

function isNullOrWhitespace( input ) {
  return !input || !input.trim();
}

Solution 6 - Javascript

You can create your own method Equivalent to

String.IsNullOrWhiteSpace(value)

function IsNullOrWhiteSpace( value) {

    if (value== null) return true;

    return value.replace(/\s/g, '').length == 0;
}

Solution 7 - Javascript

isEmptyOrSpaces(str){
    return !str || str.trim() === '';
}

Solution 8 - Javascript

When checking for white space the c# method uses the Unicode standard. White space includes spaces, tabs, carriage returns and many other non-printing character codes. So you are better of using:

function isNullOrWhiteSpace(str){
    return str == null || str.replace(/\s/g, '').length < 1;
}

Solution 9 - Javascript

isEmptyOrSpaces(str){
    return str === null || str.trim().length>0;
}

Solution 10 - Javascript

Try this out

/**
  * Checks the string if undefined, null, not typeof string, empty or space(s)
  * @param {any} str string to be evaluated
  * @returns {boolean} the evaluated result
*/
function isStringNullOrWhiteSpace(str) {
    return str === undefined || str === null
                             || typeof str !== 'string'
                             || str.match(/^ *$/) !== null;
}

You can use it like this

isStringNullOrWhiteSpace('Your String');

Solution 11 - Javascript

function isEmptyOrSpaces(str){
    return str === null || str.match(/^[\s\n\r]*$/) !== null;
}

Solution 12 - Javascript

You can try this:

do {
   var op = prompt("please input operatot \n you most select one of * - / *  ")
} while (typeof op == "object" || op == ""); 
// execute block of code when click on cancle or ok whthout input

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNate PetView Question on Stackoverflow
Solution 1 - JavascriptMadbreaksView Answer on Stackoverflow
Solution 2 - JavascriptRicardo TomasiView Answer on Stackoverflow
Solution 3 - JavascriptThiefMasterView Answer on Stackoverflow
Solution 4 - Javascriptsandre89View Answer on Stackoverflow
Solution 5 - JavascriptAaron HoffmanView Answer on Stackoverflow
Solution 6 - JavascriptShehzadView Answer on Stackoverflow
Solution 7 - JavascriptAliView Answer on Stackoverflow
Solution 8 - JavascriptJoao LemeView Answer on Stackoverflow
Solution 9 - JavascriptThalinda BandaraView Answer on Stackoverflow
Solution 10 - JavascriptRod lauro RomarateView Answer on Stackoverflow
Solution 11 - JavascriptpicoView Answer on Stackoverflow
Solution 12 - JavascripthamedalaviView Answer on Stackoverflow