How can we get rid of the JavaScript loop?


This is a for loop:

const list = ['a', 'b', 'c']
for (let i = 0; i < list.length; i++) {

}

We can usebreakKey words:

const list = ['a', 'b', 'c']
for (let i = 0; i < list.length; i++) {
  if (list[i] === 'b') break
  console.log(list[i])
}

breakIt can also be used in a for..of loop:

const list = ['a', 'b', 'c']
for (const item of list) {
  if (item === 'b') break
  console.log(item)
}

And in a period of time:

const list = ['a', 'b', 'c']
let i = 0
while (i < list.length) {
  if (i === 'b') break
  console.log(list[i])
  i++
}

ThiscontinueKeywords allow us to skip an iterationforwithfor..ofwithwhilecycle. The loop does end the iteration and will continue from the next loop.

A kindfor..inYou cannot use break for loops. It is impossible to end it this way.

Download mine for freeJavaScript beginner's manual


More js tutorials: