Wednesday, May 30, 2012

What is the best way to check for an empty string in JavaScript?


I saw this thread , but I didn't see a JavaScript specific example. Is there a simple string.Empty in JavaScript, or is it just checking for "" ?



Source: Tips4all

16 comments:

  1. If you just want to check whether there's any value, you can do

    if (strValue) {
    //do something
    }


    If you need to check specifically for an empty string over null, I would think checking against "" is your best bet, using the === operator (so that you know that it is, in fact, a string you're comparing against).

    ReplyDelete
  2. If you need to make sure that the string is not just a bunch of empty spaces (I'm assuming this is for form validation) you need to do a replace on the spaces.

    if(str.replace(/\s/g,"") == ""){
    }

    ReplyDelete
  3. For checking if a string is empty, null or undefined I use:

    function isEmpty(str) {
    return (!str || 0 === str.length);
    }


    For checking if a string is blank, null or undefined I use:

    function isBlank(str) {
    return (!str || /^\s*$/.test(str));
    }

    ReplyDelete
  4. The closest thing you can get to str.Empty (with the precondition that str is a String) is:

    if (!str.length) { ...

    ReplyDelete
  5. I would not worry too much about the most efficient method. Use what is most clear to your intention. For me that's usually strVar == "".

    EDIT: per comment from Constantin, if strVar some how ended up containing an integer 0 value, then strVar == "" returns true. So if that is a danger in your situation, you should code strVar === "", which ensures that the types are also the same.

    ReplyDelete
  6. you could also go with regexps:

    if((/^\s*$/).test(str)) { }


    Checks for strings that are either empty or filled with whitespace.

    ReplyDelete
  7. var s; // undefined
    var s = ""; // ""
    s.length // 0


    There's nothing representing an empty string in JavaScript. Do a check against either length (if you know that the var will always be a string) or against ""

    ReplyDelete
  8. All the above are good but this will be even better. use !!(not not) operator.

    if(!!str){
    some code here;
    }


    or use type casting:

    if(Boolean(str)){
    codes here;
    }


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

    ReplyDelete
  9. I usually compare to "" in Javascript.

    ReplyDelete
  10. I would say check against "".

    ReplyDelete
  11. I use :

    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

    ReplyDelete
  12. I use a combination, fastest checks are first.

    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);
    }

    ReplyDelete
  13. I usually use something like:

    if (str == "") {
    //Do Something
    }
    else {
    //Do Something Else
    }

    ReplyDelete
  14. just FYI, i think the most useful APIs for the String class are at Mozilla and javascript kit. elated.com has a tutorial on all of String's properties, methods,...

    Please note: the Mozilla link has been updated to https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String

    ReplyDelete
  15. function tell()
    {
    var pass = document.getElementById('pasword').value;
    var plen = pass.length;

    now you can check if your string is empty as like
    if(plen==0)
    {
    alert('empty');
    }
    else
    {
    alert('you entered something');
    }
    }


    <input type='text' id='pasword' />


    this is also a generic way to check if field is empty.

    ReplyDelete