Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - Check if at least one variable is true

How do I check if at least 1 out of a group of variables is true. For example:

var v1 = false;
var v2 = false;
var v3 = false;
var v4 = false;
var v5 = false;

Let's say that I have 5 buttons and the variable of v1 changes every time I click on button1 and so on. Let's suppose that I click on button4 and v4 changes to true. How do I check if at least one of the 5 variables is true. Something like this:

if(1/5 variables is true) {do something}

Should I create an array or something?

like image 727
Werl_ Avatar asked Aug 24 '26 01:08

Werl_


1 Answers

if([v1, v2, v3, v4, v5].some(item => item)) {
    //code
}
like image 123
Alex Avatar answered Aug 26 '26 14:08

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!