Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Array [Symbol.iterator]() method

JavaScript Array Symbol.iterator Method

Array Object Reference JavaScript Array object


Example

Get the iterator object of the array and loop through its output:

const

fruits

=

[

'

apple

'

,

'

banana

'

,

'

orange

'

]

;

//

  1. Get the iterator object of the array

const

iterator

=

fruits

[

Symbol

.

iterator

]

(

)

;

//

  1. Manually call the iterator's next() method

console

.

log

(

iterator

.

next

(

)

)

;

//

{ value: 'apple', done: false }

console

.

log

(

iterator

.

next

(

)

)

;

//

{ value: 'banana', done: false }

console

.

log

(

iterator

.

next

(

)

)

;

//

{ value: 'orange', done: false }

console

.

log

(

iterator

.

next

(

)

)

;

//

{ value: undefined, done: true }

//

  1. Use a for...of loop (which automatically calls the underlying Symbol.iterator)

for

(

const

fruit

of

fruits

)

{

console

.

log

(

fruit

)

;

}

//

Output:

//

apple

//

banana

//

orange

//

  1. Use the spread operator (which also relies on the iterator)

const

fruitCopy

=

[

...

fruits

]

;

console

.

log

(

fruitCopy

)

;

//

['apple', 'banana', 'orange']

Try it »


Definition and Usage

[Symbol.iterator]() is a built-in method of JavaScript arrays that returns the default iterator of the array. This method makes the array an iterable object that can be used by for...of structures such as loops.

What is an iterator (Iterator)?

Before explaining the [Symbol.iterator]() method, we first need to understand what an iterator is.

An iterator is a protocol in JavaScript that defines how to sequentially access the elements of a collection. Any object that implements the iterator protocol can be used by for...of loops, the spread operator (...), and other JavaScript features.

An iterator object must implement a next() method that returns an object containing two properties:

Basic Syntax



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

Example Code

Let us look at a simple example:

Example

const

fruits

=

[

'apple'

,

'banana'

,

'orange'

]

;

// Get the iterator of the array

const

iterator

=

fruits

[

Symbol.

iterator

]

(

)

;

console.

log

(

iterator.

next

(

)

)

;

// { value: 'apple', done: false }

console.

log

(

iterator.

next

(

)

)

;

// { value: 'banana', done: false }

console.

log

(

iterator.

next

(

)

)

;

// { value: 'orange', done: false }

console.

log

(

iterator.

next

(

)

)

;

// { value: undefined, done: true }

In this example, we:

  1. Created an array containing three kinds of fruit
  2. Pass [Symbol.iterator]() method obtained the iterator of the array
  3. Called the iterator's next() method multiple times to get each element of the array in turn

Practical Application Scenarios

1. Use a for...of loop

for...of The loop internally automatically calls the array's [Symbol.iterator]() method in the component:

Example

const

colors

=

[

'red'

,

'green'

,

'blue'

]

;

for

(

const

color of colors

)

{

  console.

log

(

color

)

;

}

// Output:

// red

// green

// blue

2. Use the spread operator

The spread operator also relies on the iterator protocol:

Example

const

numbers

=

[

1

,

2

,

3

]

;

const

copiedNumbers

=

[

...

numbers

]

;

console.

log

(

copiedNumbers

)

;

// [1, 2, 3]

3. Destructuring assignment

Array destructuring also uses the iterator:

Example

const

[

first

,

second

]

=

[

'a'

,

'b'

,

'c'

]

;

console.

log

(

first

)

;

// 'a'

console.

log

(

second

)

;

// 'b'


Customize array iteration behavior

Although this is rarely needed, you can override the array's default [Symbol.iterator]() method to customize the iteration behavior:

Example

const

customArray

=

[

1

,

2

,

3

]

;

customArray

[

Symbol.

iterator

]

=

function

(

)

{

  let index

=

this

.

length

;

 

 

return

{

    next

:

(

)

=>

{

      index

--;

     

return

{

        value

:

this

[

index

]

,

        done

:

index

<

0

}

;

   

}

 

}

;

}

;

for

(

const

num of customArray

)

{

  console.

log

(

num

)

;

}

// Output:

// 3

// 2

// 1

In this example, we modified the iterator so that the array iterates from back to front.


Comparison with other iteration methods

Difference from forEach()

Relationship with keys()/values()/entries()

These methods all return iterators, but they have different purposes:


Browser compatibility

Array [Symbol.iterator]() The method is supported in the following environments:


Summary

Array [Symbol.iterator]() The method is key to JavaScript arrays implementing the iterable protocol. Understanding how this method works can help you:

  1. Understand the JavaScript iteration mechanism more deeply
  2. Write more efficient iteration code
  3. Customize iteration behavior when needed

Remember, in most cases you do not need to call this method directly, because for...of loops and other language features handle it automatically internally. But understanding how it works will make you a better JavaScript developer.

Array Object Reference JavaScript Array object