Skip to main content

How to Loop Over an Array with for...of in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
for (const <item> of <iterable>) {
<statements>
}
PartWhat 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:

main.js, which creates an array of the seats A1, A2 and A3, logs each seat in a for...of loop, then logs each letter of the string Hi! in a second for...of loop.

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​

Terminal
node main.js
Output
A1
A2
A3
H
i
!

A zsh terminal where node main.js prints A1, A2 and A3, then H, i and the exclamation mark, each on its own line.

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, seat held the next item, A1, then A2, then A3.
  • The second loop ran once for each character of 'Hi!', the ! included.
  • The loops never mention an index or a length. for...of starts at the first item and stops after the last one by itself.

Common Mistakes​

MistakeWhat happensDo this instead
Writing in instead of of, for (const i in seats)You get the indexes as strings, '0', '1' and '2', not the itemsUse of for the items
Looping over a plain object, for (const s of settings)TypeError: settings is not iterableLoop over Object.entries(settings), which gave theme dark for { theme: 'dark' }
Assigning to a const item inside the loopTypeError: Assignment to constant variable.Put the new value in a new variable
Expecting a changed item to change the arrayWith let seat, seat = seat.toLowerCase() left seats as [ 'A1', 'A2', 'A3' ]Change the array itself, for example by index

Troubleshooting​

SymptomLikely causeFix
TypeError: null is not iterableThe value you loop over is null, often because something wasn't foundCheck the value before the loop
TypeError: ... is not iterable for an objectPlain objects can't be looped over with for...ofUse Object.entries, Object.keys or Object.values
The loop prints 0, 1, 2It's a for...in loopChange 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​