Here is a for loop:

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

}

We can break at any point in time the execution using the break keyword:

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

break also works in for…of loops:

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

#javascript #programming #loop

What are the ways we can break out of a loop in JavaScript?
1.45 GEEK