Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Symbol
  6. iterator

Symbol.iterator

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.

TheSymbol.iterator static data property represents thewell-known symbolSymbol.iterator. Theiterable protocol looks up this symbol for the method that returns the iterator for an object. In order for an object to be iterable, it must have a[Symbol.iterator] key.

Try it

const iterable = {};iterable[Symbol.iterator] = function* () {  yield 1;  yield 2;  yield 3;};console.log([...iterable]);// Expected output: Array [1, 2, 3]

Value

The well-known symbolSymbol.iterator.

Property attributes ofSymbol.iterator
Writableno
Enumerableno
Configurableno

Description

Whenever an object needs to be iterated (such as at the beginning of afor...of loop), its[Symbol.iterator]() method is called with no arguments, and the returnediterator is used to obtain the values to be iterated.

Some built-in types have a default iteration behavior, while other types (such asObject) do not. Some built-in types with a[Symbol.iterator]() method are:

See alsoIteration protocols for more information.

Examples

User-defined iterables

We can make our own iterables like this:

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

Or iterables can be defined directly inside a class or object using acomputed property:

js
class Foo {  *[Symbol.iterator]() {    yield 1;    yield 2;    yield 3;  }}const someObj = {  *[Symbol.iterator]() {    yield "a";    yield "b";  },};console.log(...new Foo()); // 1, 2, 3console.log(...someObj); // 'a', 'b'

Non-well-formed iterables

If an iterable's[Symbol.iterator]() method does not return an iterator object, then it is a non-well-formed iterable. Using it as such is likely to result in runtime exceptions or buggy behavior:

js
const nonWellFormedIterable = {};nonWellFormedIterable[Symbol.iterator] = () => 1;[...nonWellFormedIterable]; // TypeError: [Symbol.iterator]() returned a non-object value

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-symbol.iterator

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp