There is two possibility to interact in a for loop in Javascript:
break
,continue
.For example, to stop a for loop it will be :
for (i = 0; i < 8; i++) {if (i === 3) {break}console.log(`We are in the ${i} loop. Loop after 2 will not show`)}
To simply jump the fourth loop, we can do :
for (i = 0; i < 8; i++) {if (i === 3) {continue}console.log(`We are in the ${i} loop. Loop after 3 will be ignored`)}
Those also work with for ... of
loop :
const list = ['a', 'b', 'c']for (const value of list) {if (value === 'a') {continue}console.log(`The value are ${value}, a will be ignored`)}
Note that those will not work with
forEach
statement.