As a condition in for or while loops, can you use a statement without "i"?
For example:
for (let i = 0 ; myArray.length =< 7; i++)
I want the loop to run until my condition is met, but not sure if I can achieve that without including the "i" there.
for ( initiaizer; condition; increment) {
body
}
is the same as:
initializer
while (condition) {
body
increment
}
And you can follow the same rules in each. Meaning initializer and increment are actually optional (for (; false; ) {}
is valid). Creating and incrementing a variable called i
is just a useful convention and has no special meaning.
For loops were invented to simplify while loops in the common case where you want to basically count from one number to another. If you’re not doing that, a while loop might be better.