[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/for...of [Back]  [Original]

for...of - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

for...of

Baseline

20157

for...of Array, String, TypedArray, Map, Set, NodeList DOM arguments

const array = ["a", "b", "c"];

for (const element of array) {
  console.log(element);
}

// : "a"
// : "b"
// : "c"

js
for (variable of iterable)
  statement
variable

const, let, var, using, await using var for...of

iterable

statement

variable

for...of 1

for...of [Symbol.iterator]() next() variable

for...of next() done: true statement

for...of break return()

for...of variable = const 2 let

js
const iterable = [10, 20, 30];

for (let value of iterable) {
  value += 1;
  console.log(value);
}
// 11
// 21
// 31

:

using await using await using await

js
const resources = [dbConnection1, dbConnection2, dbConnection3];

for (using dbConnection of resources) {
  dbConnection.query("...");
  // dbConnection 
}

for (x.y of iterable)

async

js
let async;
for (async of [1, 2, 3]); // SyntaxError: The left-hand side of a for-of loop may not be 'async'.

for for (async of => {};)

using of

js
for (using of of []); // SyntaxError

using for (using of [])

Array

js
const iterable = [10, 20, 30];

for (const value of iterable) {
  console.log(value);
}
// 10
// 20
// 30

Unicode

js
const iterable = "boo";

for (const value of iterable) {
  console.log(value);
}
// "b"
// "o"
// "o"

js
const iterable = new Uint8Array([0x00, 0xff]);

for (const value of iterable) {
  console.log(value);
}
// 0
// 255

Map

js
const iterable = new Map([
  ["a", 1],
  ["b", 2],
  ["c", 3],
]);

for (const entry of iterable) {
  console.log(entry);
}
// ['a', 1]
// ['b', 2]
// ['c', 3]

for (const [key, value] of iterable) {
  console.log(value);
}
// 1
// 2
// 3

Set

js
const iterable = new Set([1, 1, 2, 2, 3, 3]);

for (const value of iterable) {
  console.log(value);
}
// 1
// 2
// 3

arguments

arguments JavaScript

js
function foo() {
  for (const value of arguments) {
    console.log(value);
  }
}

foo(1, 2, 3);
// 1
// 2
// 3

NodeList

read <article> DOM NodeList

js
const articleParagraphs = document.querySelectorAll("article > p");
for (const paragraph of articleParagraphs) {
  paragraph.classList.add("read");
}

[Symbol.iterator]()

js
const iterable = {
  [Symbol.iterator]() {
    let i = 1;
    return {
      next() {
        if (i <= 3) {
          return { value: i++, done: false };
        }
        return { value: undefined, done: true };
      },
    };
  },
};

for (const value of iterable) {
  console.log(value);
}
// 1
// 2
// 3

[Symbol.iterator]()

js
const iterable = {
  *[Symbol.iterator]() {
    yield 1;
    yield 2;
    yield 3;
  },
};

for (const value of iterable) {
  console.log(value);
}
// 1
// 2
// 3

this [Symbol.iterator]() for...of

js
let i = 1;

const iterator = {
  next() {
    if (i <= 3) {
      return { value: i++, done: false };
    }
    return { value: undefined, done: true };
  },
  [Symbol.iterator]() {
    return this;
  },
};

for (const value of iterator) {
  console.log(value);
}
// 1
// 2
// 3

js
function* source() {
  yield 1;
  yield 2;
  yield 3;
}

const generator = source();

for (const value of generator) {
  console.log(value);
}
// 1
// 2
// 3

break 2

js
const source = [1, 2, 3];

const iterator = source[Symbol.iterator]();

for (const value of iterator) {
  console.log(value);
  if (value === 1) {
    break;
  }
  console.log("");
}
// 1

// 
// 
for (const value of iterator) {
  console.log(value);
}
// 2
// 3

// 
// 
for (const value of iterator) {
  console.log(value);
}
// []

return()

js
function* source() {
  yield 1;
  yield 2;
  yield 3;
}

const generator = source();

for (const value of generator) {
  console.log(value);
  if (value === 1) {
    break;
  }
  console.log("");
}
// 1

// 
// 
for (const value of generator) {
  console.log(value);
}
// []

for...of for...in

for...in for...of

for...in for...of

Array for...of for...in

js
Object.prototype.objCustom = function () {};
Array.prototype.arrCustom = function () {};

const iterable = [3, 5, 7];
iterable.foo = "hello";

for (const i in iterable) {
  console.log(i);
}
// "0", "1", "2", "foo", "arrCustom", "objCustom"

for (const i in iterable) {
  if (Object.hasOwn(iterable, i)) {
    console.log(i);
  }
}
// "0" "1" "2" "foo"

for (const i of iterable) {
  console.log(i);
}
// 3 5 7

iterable objCustom arrCustom Object.prototype Array.prototype

for...in iterable 3, 5, 7 "hello" arrCustom objCustom for...in

2 Object.hasOwn() 0, 1, 2, foo arrCustom objCustom

for...of iterable 3, 5, 7

ECMAScript 2027 LanguageSpecification
# sec-for-in-and-for-of-statements


Web Proxy Viewer  |  New URL  |  Original Page