Is there any way to assign a variable as the conditional operator in a for
loop in Javascript?
For example, take this for
loop:
for(var i = 0, j = 5; i < 10; i++, j++) {
console.log(i + ", " + j);
}
I want to be able to set the i < 10
part as a variable. I might want the for loop to run until j < 8
instead of i < 10
. The way I am currently doing it is using an if
statement and then using two different for
loops with the exact same code except for the conditional, and I just wanted to know if a more efficient way was possible. Thanks!