JavaScript for...of

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

for...of loops the values of an iterable. Arrays, strings, Maps, Sets, and anything with Symbol.iterator work.

JavaScript Syntax

JavaScript
for (const color of ["red", "green"]) {
  console.log(color);
}
Try it Editor

How It Works

  1. Values, not indexes. color is "red", then "green".
  2. Uses the iterator. Same idea as array.values().

Examples

Example 1 — Loop a string

Each UTF-16 code unit is a value (emoji may be two).

JavaScript
for (const ch of "Hi") console.log(ch);
Try it Editor

How It Works

  1. Strings are iterable.
  2. Each character prints.

Example 2 — Need indexes?

Use entries() or keys().

JavaScript
for (const [i, v] of ["a", "b"].entries()) {
  console.log(i + ":" + v);
}
Try it Editor

How It Works

  1. entries yields [index, value] pairs.
  2. Destructure into i and v.

Example 3 — Map values

for...of on a Map yields [key, value] pairs.

JavaScript
const m = new Map([["x", 1]]);
for (const [k, v] of m) console.log(k, v);
Try it Editor

How It Works

  1. Map is iterable.
  2. Each entry is a two-item array.

Frequently Asked Questions

No. Objects are not iterable by default. Use Object.keys/values/entries.
No. It reads values through the iterator.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful