Friday, May 4, 2012

Is there a standard function to check for null, undefined, or blank variables in JavaScript?


Is there a universal JavaScript function that checks that a variable has a value and ensures that it's not undefined or null ? I've got this code, but I'm not sure if it covers all cases:




function isEmpty(val){
return (val === undefined || val == null || val.length <= 0) ? true : false;
}


Source: Tips4all

7 comments:

  1. You can just check if the variable has a truthy value or not. That means

    if( value ) {
    }


    will evaluate to true if value is not:


    null
    undefined
    NaN
    empty string ("")
    0
    false


    The above list represents all possible falsy values in ECMA-/Javascript. Furthermore, if you don't know whether a variable exists (that means, if it was declared) you should check with the typeof operator. For instance

    if( typeof foo !== 'undefined' ) {
    // foo could get resolved and it's defined
    }


    If you can be sure that a variable is declared at least, you should directly check if it has a truthy value like shown above.

    Further read: http://typeofnan.blogspot.com/2011/01/typeof-is-fast.html

    ReplyDelete
  2. It's really about what you need, I use something like:

    self.empty = function () {
    if (
    element === "" ||
    element === 0 ||
    element === "0" ||
    element === null ||
    element === "NULL" ||
    element === undefined ||
    element === false
    ) {
    return true;
    }
    if (typeof(element) === 'object') {
    var i = 0;
    for (key in element) {
    i++;
    }
    if (i === 0) { return true; }
    }
    return false;
    }


    But, honestly, that's a bit more broad than I would recommend for most, it just happened to evolve based on the integration needs with other people's code over time. And, I'll even note that it really doesn't handle Arrays as well as it should. The best thing to do is give yourself some personal context and make sure you cover it.

    ReplyDelete
  3. function isEmpty(value){
    return (value == null || value.length === 0);
    }


    This will return true for

    undefined // Because undefined == null

    null

    []

    ""


    and zero argument functions since a function's length is the number of declared parameters it takes.

    To disallow the latter category, you might want to just check for blank strings

    function isEmpty(value){
    return (value == null || value.length === '');
    }

    ReplyDelete
  4. // for null and undefined
    return typeof value === "undefined" || value === null;

    ReplyDelete
  5. You are a bit overdoing it. To check if a variable is not given a value, you would only need to check against undefined and null.

    function isEmpty(value){
    return (typeof value === "undefined" || value === null);
    }


    This is assuming 0, "", and objects(even empty object and array) are valid "values".

    ReplyDelete
  6. If the variable hasn't been declared, you wont be able to test for undefined using a function because you will get an error.

    if (foo) {}
    function (bar) {}(foo)


    Both will generate an error if foo has not been declared.

    If you want to test if a variable has been declared you can use

    typeof foo != "undefined"


    if you want to test if foo has been declared and it has a value you can use

    if (typeof foo != "undefined" && foo) {
    //code here
    }

    ReplyDelete
  7. you can use:

    If clause to validate if the string or value is not empty.
    like this:

    if (someVar.value)
    {
    //its not emppty
    }
    else
    {
    //Its empty
    }

    ReplyDelete