Answered
I have a JavaScript for
loop that looks like this:
for (let i=0; i < array.length; i++) {
if (array.points < 10) {
// do something
} else {
// stop the for loop here
}
}
How do I stop the for loop from continuing in the example code I provided above?
The break
statement will do the trick for you.
Update your code to the following:
for (let i=0; i < array.length; i++) {
if (array.points < 10) {
// do something
} else {
break
}
}
Use the break
statement:
break
It jumps out of the loop and stops any additional iterations.