In JavaScript, an iterable is an object that defines how it can be iterated over. Iterables are a fundamental concept in modern JavaScript and are used for looping over the elements or values contained within the object. To be considered an iterable, an object must implement the iterator protocol.
The iterator protocol involves having a special method, Symbol.iterator, which returns an iterator object. This iterator object, in turn, should have a
next() method that is used to retrieve the next value in the sequence.
Here's what defines an iterable in JavaScript:
Symbol.iterator Method: An iterable object must have a
Symbol.iterator method. When called, this method returns an iterator object.
Iterator Object: The iterator object returned by Symbol.iterator should have a
next() method. The next() method is used to retrieve the next value in the iteration.
next() Method: The next() method returns an object with two properties:
value: This property contains the current value of the iteration.
done: This property is a boolean that indicates whether the iteration is complete (true) or not (false).
Examples of built-in iterables in JavaScript include arrays, strings, maps, and sets. Here's how you can identify and use an iterable:
const array = [1, 2, 3];
const string = "Hello";
const map = new Map([["key1", "value1"], ["key2", "value2"]]);
for (const element of array) {
console.log(element);
}
for (const char of string) {
console.log(char);
}
for (const [key, value] of map) {
console.log(key, value);
}
In these examples, array, string, and
map are all iterable objects. You can use a for...of loop to easily iterate over their elements or key-value pairs. These objects adhere to the iterator protocol, which makes them iterable.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In JavaScript, an iterable is an object that defines how it can be iterated over. Iterables are a fundamental concept in modern JavaScript and are used for looping over the elements or values contained within the object. To be considered an iterable, an object must implement the iterator protocol.
The iterator protocol involves having a special method, Symbol.iterator, which returns an iterator object. This iterator object, in turn, should have a next() method that is used to retrieve the next value in the sequence.
Here's what defines an iterable in JavaScript:
Symbol.iterator Method: An iterable object must have a Symbol.iterator method. When called, this method returns an iterator object.
Iterator Object: The iterator object returned by Symbol.iterator should have a next() method. The next() method is used to retrieve the next value in the iteration.
next() Method: The next() method returns an object with two properties:
Examples of built-in iterables in JavaScript include arrays, strings, maps, and sets. Here's how you can identify and use an iterable:
In these examples, array, string, and map are all iterable objects. You can use a for...of loop to easily iterate over their elements or key-value pairs. These objects adhere to the iterator protocol, which makes them iterable.