JavaScript check if value is only undefined, null or false

Lime picture Lime · Jul 26, 2011 · Viewed 142.3k times · Source

Other than creating a function, is there a shorter way to check if a value is undefined,null or false only in JavaScript?

The below if statement is equivalent to if(val===null && val===undefined val===false) The code works fine, I'm looking for a shorter equivalent.

if(val==null || val===false){
  ;
}

Above val==null evaluates to true both when val=undefined or val=null.

I was thinking maybe using bitwise operators, or some other trickery.

Answer

hugomg picture hugomg · Jul 27, 2011

Well, you can always "give up" :)

function b(val){
    return (val==null || val===false);
}