# How to break out of a for loop in JavaScript

> Learn how to break out of a for or for..of loop in JavaScript with the break statement, and why you cannot break out of a forEach loop the same way.

Author: Flavio Copes | Published: 2019-09-11 | Canonical: https://flaviocopes.com/how-to-break-for-loop-javascript/

Say you have a `for` loop:

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

If you want to break at some point, say when you reach the element `b`, you can use the `break` statement:

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

You can use `break` also to break out of a for..of loop:

```js
const list = ['a', 'b', 'c']

for (const value of list) {
  console.log(value)
  if (value === 'b') {
    break
  }
}
```

> Note: there is no way to break out of a `forEach` loop, so (if you need to) use either `for` or `for..of`.
