Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

of — contextual keyword in loops

`of` separates the loop variable from the source of values in `for...of`. It is a contextual keyword, not a reserved word: outside that role, it can be used as a variable name.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

for (const value of iterable) { /* statements */ }

of has a syntactic role in for...of, but it is neither a standalone operator nor a reserved word. const of = 3; is valid: its meaning depends on where it appears.

In for (const value of iterable), the loop obtains successive values from the iterator of iterable. Arrays provide their elements this way. A plain object without the iteration protocol is not a suitable source and causes a TypeError.

Do not confuse for...of with for...in: the former reads iterator values, whereas the latter enumerates enumerable string property keys, including inherited ones. For an array, element values are not the same as index keys.

of also appears in for await...of, the variant supporting asynchronous iteration. In the example, the ordinary loop adds 2, 4, and 6; const value creates a separate binding for each iteration, so it does not prevent moving to subsequent values.

Examples

const numbers = [2, 4, 6];
let total = 0;
for (const value of numbers) {
  total += value;
}
console.log(total);

Result

12

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources