Checking if multiple variables is not null, undefined or empty in an efficient way

Because if(variable) ignores any falsy value, this will work for you

if(variable && variable2 && variable3)

The following values are always falsy in JS:

false.
0 (zero)
"" (empty string)
null.
undefined.
NaN (a special Number value meaning Not-a-Number!)

Update:-

If there is a case when you want to execute if block even if the value is 0, you have to add an extra check saying either 0 or some other value.

if((variable || variable === 0) && (variable2 || variable2 === 0) && (variable3 || variable3 === 0))

If your variables are containing some values that are truthy, like a string, and that is considered positive in your condition, then you could simply check using Array.prototype.every()...

if (![var1, var2, var3].every(Boolean)) {
     throw new exc;
}

Which will check to ensure every variable has a truthy value.


If IE support matter to you (IE9+), you could use the following solution.

You could use Array.prototype.some(). In a nutshell this array method will return true if any of the elements in the array meet a certain condition. This condition will be el == null this will work because undefined == null and null == null both resolve in true

See this stackoverflow post for more information

Which equals operator (== vs ===) should be used in JavaScript comparisons?

Array.prototype.some() browser support (93% time of writing) here

var a = 5;
var b = null;
var c = undefined;

if ( [a,b,c].some(el => el == null) ) {          
  console.log("You f*d up")
}

Or you could use Array.prototype.includes()

See support (93% time of writing) here here

var a = 5;
var b = null;
var c = undefined;
   
 if ( [a,b,c].includes(undefined) || [a,b,c].includes(null) ) {          
   console.log("You f*d up")
 }

If you want good browser support and you don't mind an external library, use lodash.

var a = 5;
var b = null;
var c = undefined;

if ( _.some([a,b,c], el => _.isNil(el)) ) {
  console.log("You f*d up")
}
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>