21

At the moment I have an if condition like this:

if(
  (variable != null && variable != '' && !variable) &&
  (variable2 != null && variable2 != '' && !variable2) &&
  (variable3 != null && variable3 != '' && !variable3)
  //etc..
)

I need to use it to check if multiple variables have value (were not left out), but I feel like this is a messy solution and wanted to ask if there is a more efficient way? Maybe additional checks as well?

Ilja
  • 40,784
  • 74
  • 242
  • 455
  • 1
    I think `if(var)` will do the job. (This will test if the variable contains something) – Emanuel Vintilă Sep 17 '15 at 08:17
  • Possible duplicate of [How do you check for an empty string in JavaScript?](https://stackoverflow.com/questions/154059/how-do-you-check-for-an-empty-string-in-javascript) – T.Todua Dec 04 '18 at 09:33

7 Answers7

55

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))
Mritunjay
  • 24,184
  • 7
  • 51
  • 67
  • But if var = 0 it will considered false too – pmiranda Mar 14 '17 at 16:31
  • 1
    Seems to work for testing false too? (this might save someone 2 minutes): `var variable = ""; var variable2 = ""; var variable3 = ""; if (!variable && !variable2 && !variable3) { console.log("hi") }` – user1063287 Oct 14 '17 at 08:14
12

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>
realappie
  • 4,205
  • 2
  • 26
  • 38
8

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.

alex
  • 460,746
  • 196
  • 858
  • 974
4

I assume you are checking for truthy values? If so, you can just do:

variable && variable2 && variable3

Lee
  • 2,781
  • 3
  • 20
  • 26
0

See JavaScript: What is the difference between if (!x) and if (x == null)?

!variable will be true for all falsy values so you only have to

if(variable1 && variable2 && variable3 && ...)
Community
  • 1
  • 1
SiCK
  • 377
  • 4
  • 15
0

Long hand -

 if (var1 !== null || var1 !== undefined || var1 !== '') {
   let var2 = var1;
 }

Short hand -

 const var2 = var1  || '';
0
let variable null;
let variable2 undefined;
let variable3 'string';
let variable4 '';
let variable5 true;
let variable6 false;
let variable7 0;
let variable8 1;

Validating multiple values using the filter function. One could expect this example to return true as the count would be 5 for (variable, variable2, variable4, variable6, variable7)

if([variable, variable2, variable3, variable4, variable5, variable6, variable7, variable8]
  .filter(q=>!q).length > 0) {
   //take action
}
chri3g91
  • 824
  • 11
  • 13