Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The for...of statement executes a loop that operates on a sequence of values sourced from an iterable object. Iterable objects include instances of built-ins such as Array, String, TypedArray, Map, Set, NodeList (and other DOM collections), as well as the arguments object, generators produced by generator functions, and user-defined iterables.
Receives a value from the sequence on each iteration. May be either a declaration with const, let, var, using, await using, or an assignment target (e.g., a previously declared variable, an object property, or a destructuring pattern). Variables declared with var are not local to the loop, i.e., they are in the same scope the for...of loop is in.
iterableAn iterable object. The source of the sequence of values on which the loop operates.
statementA statement to be executed on every iteration. May reference variable. You can use a block statement to execute multiple statements.
A for...of loop operates on the values sourced from an iterable one by one in sequential order. Each operation of the loop on a value is called an iteration, and the loop is said to iterate over the iterable. Each iteration executes statements that may refer to the current sequence value.
When a for...of loop iterates over an iterable, it first calls the iterable's [Symbol.iterator]() method, which returns an iterator, and then repeatedly calls the resulting iterator's next() method to produce the sequence of values to be assigned to variable.
A for...of loop exits when the iterator has completed (the next() result is an object with done: true). Like other looping statements, you can use control flow statements inside statement:
If the for...of loop exited early (e.g., a break statement is encountered or an error is thrown), the return() method of the iterator is called to perform any cleanup.
The variable part of for...of accepts anything that can come before the = operator. You can use const to declare the variable as long as it's not reassigned within the loop body (it can change between iterations, because those are two separate variables). Otherwise, you can use let.
Note: Each iteration creates a new variable. Reassigning the variable inside the loop body does not affect the original value in the iterable (an array, in this case).
Variables declared using the using or await using declaration are disposed every time a loop iteration is done (and await using causes an implicit await at the end of the iteration). However, if the loop early-exits, any values left in the iterator that haven't been visited are not disposed (although the current value is).
You can use destructuring to assign multiple local variables, or use a property accessor like for (x.y of iterable) to assign the value to an object property.
However, a special rule forbids using async as the variable name. This is invalid syntax:
This is to avoid syntax ambiguity with the valid code for (async of => {};;), which is a for loop.
Similarly, if you use the using declaration, then the variable cannot be called of:
This is to avoid syntax ambiguity with the valid code for (using of []), before using was introduced.
Strings are iterated by Unicode code points.
You can iterate over the arguments object to examine all parameters passed into a function.
The following example adds a read class to paragraphs that are direct descendants of the <article> element by iterating over a NodeList DOM collection.
Iterating over an object with a [Symbol.iterator]() method that returns a custom iterator:
Iterating over an object with a [Symbol.iterator]() generator method:
Iterable iterators (iterators with a [Symbol.iterator]() method that returns this) are a fairly common technique to make iterators usable in syntaxes expecting iterables, such as for...of.
Execution of the break statement in the first loop causes it to exit early. The iterator is not finished yet, so the second loop will continue from where the first one stopped at.
Generators implement the return() method, which causes the generator function to early return when the loop exits. This makes generators not reusable between loops.
Both for...in and for...of statements iterate over something. The main difference between them is in what they iterate over.
The for...in statement iterates over the enumerable string properties of an object, while the for...of statement iterates over values that the iterable object defines to be iterated over.
The following example shows the difference between a for...of loop and a for...in loop when used with an Array.
The object iterable inherits the properties objCustom and arrCustom because it contains both Object.prototype and Array.prototype in its prototype chain.
The for...in loop logs only enumerable properties of the iterable object. It doesn't log array elements 3, 5, 7 or "hello" because those are not properties — they are values. It logs array indexes as well as arrCustom and objCustom, which are actual properties. If you're not sure why these properties are iterated over, there's a more thorough explanation of how array iteration and for...in work.
The second loop is similar to the first one, but it uses Object.hasOwn() to check if the found enumerable property is the object's own, i.e., not inherited. If it is, the property is logged. Properties 0, 1, 2 and foo are logged because they are own properties. Properties arrCustom and objCustom are not logged because they are inherited.
The for...of loop iterates and logs values that iterable, as an array (which is iterable), defines to be iterated over. The object's elements 3, 5, 7 are shown, but none of the object's properties are.
| ECMAScript® 2027 Language Specification # sec-for-in-and-for-of-statements |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Oct 30, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.