JavaScript Array Symbol.iterator Method
Example
Get the iterator object of the array and loop through its output:
const
fruits
=
[
'
apple
'
,
'
banana
'
,
'
orange
'
]
;
//
- Get the iterator object of the array
const
iterator
=
fruits
[
Symbol
.
iterator
]
(
)
;
//
- 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 }
//
- Use a for...of loop (which automatically calls the underlying Symbol.iterator)
for
(
const
fruit
of
fruits
)
{
console
.
log
(
fruit
)
;
}
//
Output:
//
apple
//
banana
//
orange
//
- 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:
valuevalue: the current iterated valuedonedone: a Boolean indicating whether the iteration is complete
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:
- Created an array containing three kinds of fruit
- Pass
[Symbol.iterator]()method obtained the iterator of the array - 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()
forEach()is an array method that executes a callback function for each element[Symbol.iterator]()returns an iterator object, allowing more flexible control over the iteration process
Relationship with keys()/values()/entries()
These methods all return iterators, but they have different purposes:
keys()keys(): an iterator that returns array indicesvalues()values(): an iterator that returns array elements (same as[Symbol.iterator]()the same)entries()entries(): an iterator that returns index/value pairs
Browser compatibility
Array [Symbol.iterator]() The method is supported in the following environments:
- Chrome 38+
- Firefox 36+
- Edge 12+
- Safari 9+
- Opera 25+
- Node.js 0.12+
Summary
Array [Symbol.iterator]() The method is key to JavaScript arrays implementing the iterable protocol. Understanding how this method works can help you:
- Understand the JavaScript iteration mechanism more deeply
- Write more efficient iteration code
- 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.
JavaScript Array object