In JavaScript, an iterable is an object that defines a standard way to be looped over. If an object is iterable, you can use constructs like for...of, the spread syntax [...iterable], and other language features to process its values in sequence. Built-in iterables include arrays, strings, maps, sets, and more.
Symbol.iterator method.obj[Symbol.iterator]().next() method: Returns objects of the form { value, done }.for...of: Loop that works on any iterable, not just arrays.Symbol.iterator.[...iterable] converts any iterable into an array.The for...of loop is the most common way to consume iterables:
for (const item of iterable) {
// use item
}
Under the hood, JavaScript looks for the Symbol.iterator method on the object. That method must return an iterator object, which exposes a next() method. Each call to next() returns:
value: the current item in the sequence.done: true when the sequence is finished, otherwise false.for...of
const colors = ["red", "green", "blue"];
for (const color of colors) {
console.log(color);
}
// Output:
// red
// green
// blue
The array colors is iterable. The for...of loop automatically uses its built-in iterator to give you each color in order.
const text = "Hello";
for (const char of text) {
console.log(char);
}
// Output:
// H
// e
// l
// l
// o
Strings also implement Symbol.iterator, so you can loop over each character just like you loop over array elements.
You can define your own iterable by implementing the Symbol.iterator method. A convenient way is to use a generator function (* syntax).
const myIterable = {
*[Symbol.iterator]() {
yield 1;
yield 2;
yield 3;
}
};
for (const value of myIterable) {
console.log(value);
}
// Output:
// 1
// 2
// 3
The object myIterable becomes iterable because it defines [Symbol.iterator](). The generator yields values one by one, which for...of consumes.
The spread operator ... consumes any iterable and expands its values.
const str = "ABC";
const letters = [...str];
console.log(letters);
// Output:
// ["A", "B", "C"]
The string "ABC" is iterable, so [...str] creates an array of its characters.
.next()You can directly work with the iterator object and call next() yourself.
const arr = [10, 20, 30];
const iterator = arr[Symbol.iterator]();
console.log(iterator.next().value); // 10
console.log(iterator.next().value); // 20
console.log(iterator.next().value); // 30
console.log(iterator.next().done); // true
Calling arr[Symbol.iterator]() returns an iterator for the array. Each call to next() advances the internal pointer and returns the next value. Once all items are consumed, done becomes true.
Most iterable examples use console.log(). To see the real output:
for...of for values, and for...in for object keys.[...thing] or for...of, remember: that thing is an iterable.[...iterable] to quickly convert any iterable into an array for further array methods like map, filter, or reduce.for...of to loop through a string (e.g., "JavaScript") and log each character.2, 4, 6, 8, 10, then loop over it with for...of.const chars = [..."iterable"];next() on an iterator and log both value and done until the sequence finishes.