27

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
  • 44,142
  • 92
  • 275
  • 498
  • 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

62

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
  • 25,338
  • 7
  • 55
  • 68
  • But if var = 0 it will considered false too – pmiranda Mar 14 '17 at 16:31
  • 2
    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
18

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,656
  • 2
  • 29
  • 38
9

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
  • 479,566
  • 201
  • 878
  • 984
4

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

variable && variable2 && variable3

Lee
  • 2,993
  • 3
  • 21
  • 27
  • it does not return true but the last variable value if all are valid valeus.. anything to get true? – Sunil Garg Jan 20 '23 at 13:12
  • Cast the expression to a boolean `Boolean(variable && variable2)` or `!!(variable && variable2)` – Lee Jan 23 '23 at 10:36
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
  • 367
  • 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
  • 1,196
  • 14
  • 16