31

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?

2

7 Answers 7

65

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))
Sign up to request clarification or add additional context in comments.

2 Comments

But if var = 0 it will considered false too
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") }
19

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>

Comments

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.

Comments

4

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

variable && variable2 && variable3

2 Comments

it does not return true but the last variable value if all are valid valeus.. anything to get true?
Cast the expression to a boolean Boolean(variable && variable2) or !!(variable && variable2)
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 && ...)

Comments

0

Long hand -

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

Short hand -

 const var2 = var1  || '';

Comments

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.