Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jake214's avatar

Javascript Logical Operator AND inside 'if' condition

i just want a suggestions if there's any simpler way to do this? or there is no other way. thanks

if(var1 == "" || var2 == "" || var3 == "" || var4 == "" || var5 == "" || var6 == "" || var7 == "" || var8 == ""){
  alert("null value");
} else {
  alert("good");
}
0 likes
6 replies
Sinnbeck's avatar

How about

if([var1, var2, var3, var4, var5, var6, var7, var8].filter((x) => x === '').length) {
  alert("null value");
} else {
  alert("good");
}
1 like
OussamaMater's avatar
Level 37

If any of the variables is empty, then it alerts the null value otherwise everything is good to go.

if ([var1, var2, var3, var4, var5, var6, var7, var8].some(el => el == "")) {
	alert("null value");
} else {
    alert("good");
}
2 likes
OussamaMater's avatar

@Sinnbeck both works tho!

@jake214 please close the thread if one of the solutions helped you by setting a best answer to one of the replies (both do the job and are a great refactor to the older version I guess :) )

Please or to participate in this conversation.