# Loop Over an Array in JavaScript with for...of

> Iterate over array elements in JavaScript using the modern for...of statement, a cleaner alternative to traditional for loops and forEach callbacks

**URL:** https://sentry.io/answers/loop-over-an-array-in-javascript/

---

## The problem

What is the best way to loop over an array in JavaScript?

## The solution

Looping over arrays in JavaScript was historically done using either a standard `for` loop or the [`Array.prototype.forEach()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) loop. The former approach loops over indexes rather than items, and the latter approach can sometimes behave unexpectedly. Modern JavaScript provides the [`for...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) statement as a simple way to loop over array items.

```javascript
const numbers = [1, 2, 3];

for (const number of numbers) {
  console.log(number);
}

// output:
// 1
// 2
// 3
```

---

*Source: [sentry.io/answers/loop-over-an-array-in-javascript/](https://sentry.io/answers/loop-over-an-array-in-javascript/)*
