WebMasterCampus
WEB DEVELOPER Resources

JS Check Null Undefined or Blank Variables

JS Check Null Undefined or Blank Variables


In JavaScript we can check if the variable has a false value. Following are the possible false values in JavaScript.

    null
    undefined
    NaN
    empty string ("")
    0
    false

will evaluate to true if value is not

    if( !value ) {
        //Code to execute if false
    }

All possible falsy values in ECMA-/Javascript. Find it in the specification at the ToBoolean section.

Example

    <h1>JavaScript Check if Variable value is false or not true</h1>

    <script>
        a = undefined;
        b = NaN;
        c = "";
        d = null;
        e = 0;
        f = false;

        if (!a) {
            console.log("a: " + a);
        }
        if (!b) {
            console.log("b: " + b);
        }
        if (!c) {
            console.log("c: " + c);
        }
        if (!d) {
            console.log("d: " + d);
        }
        if (!e) {
            console.log("e: " + e);
        }
        if (!f) {
            console.log("f: " + f);
        }
    </script>
Created with love and passion.