How to Loop Over an Array with for...of in JavaScript
for (const item of array) { ... } runs the code between the braces once for every item of the array, in order, with item holding the current one. It works on anything JavaScript can step through, such as strings, so it's the simplest way to visit each item.
Syntax
for (const <item> of <iterable>) {
<statements>
}
| Part | What it does |
|---|---|
<item> | A name for the current item. It holds a different item on each pass, starting with the first. |
<iterable> | What to loop over: an array, a string, or any other value JavaScript can step through. MDN calls these values iterable. |
<statements> | The code to run for each item. It can use <item>. |
const is the usual choice, because the loop gives <item> a fresh value on each pass rather than changing it. MDN's for...of reference lists the values it can loop over.
The Example
The example loops over an array of three seat names and prints each one. A second loop goes over the string 'Hi!', to show that a string is stepped through one character at a time.
Prerequisites
- Node.js. This guide uses Node.js 26.13.1.
Step 1: Write the Code
Create a file named main.js:

const seats = ['A1', 'A2', 'A3'];
for (const seat of seats) {
console.log(seat);
}
for (const letter of 'Hi!') {
console.log(letter);
}
Step 2: Run It
node main.js
A1
A2
A3
H
i
!

The command is the same in zsh, bash and PowerShell.
What Happened
- The first loop ran three times, once for each item of
seats. On each pass,seatheld the next item,A1, thenA2, thenA3. - The second loop ran once for each character of
'Hi!', the!included. - The loops never mention an index or a length.
for...ofstarts at the first item and stops after the last one by itself.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing in instead of of, for (const i in seats) | You get the indexes as strings, '0', '1' and '2', not the items | Use of for the items |
Looping over a plain object, for (const s of settings) | TypeError: settings is not iterable | Loop over Object.entries(settings), which gave theme dark for { theme: 'dark' } |
Assigning to a const item inside the loop | TypeError: Assignment to constant variable. | Put the new value in a new variable |
| Expecting a changed item to change the array | With let seat, seat = seat.toLowerCase() left seats as [ 'A1', 'A2', 'A3' ] | Change the array itself, for example by index |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: null is not iterable | The value you loop over is null, often because something wasn't found | Check the value before the loop |
TypeError: ... is not iterable for an object | Plain objects can't be looped over with for...of | Use Object.entries, Object.keys or Object.values |
The loop prints 0, 1, 2 | It's a for...in loop | Change in to of |
FAQ
How do I get the index as well as the item?
Loop over array.entries(), which gives each item with its index: for (const [index, seat] of seats.entries()) printed 0 A1, 1 A2 and 2 A3.
How do I stop a loop early?
Use break. A loop with if (seat === 'A2') break; before its console.log printed only A1. continue skips to the next item instead.
What else can I loop over with for...of?
Anything iterable: arrays, strings, Map and Set objects, and the lists of elements a browser's querySelectorAll returns. A Set built from ['x', 'y', 'x'] gave x and y, because a Set keeps each value once.
Conclusion
for (const item of array) visits every item in order, without indexes or lengths to get wrong. Use of for items, not in, and turn a plain object into an array with Object.entries before you loop over it.
Additional Resources
- for...of, on MDN.
- Iteration protocols, on MDN.
- Object.entries(), on MDN.
