Loading lesson path
Iterable can be iterated over with for...of loops The for...of Loop The JavaScript for..of statement loops through the elements of an iterable object.
Syntax for (variable of iterable) {// code block to be executed
}Iterating means looping over a sequence of elements.
You can use a for...of loop to iterate over the elements of a string:
const name = "W3Schools";
for (const x of name) {// code block to be executed
}You can use a for...of loop to iterate over the elements of an Array:
Example 1 const letters = ["a","b","c"];
for (const x of letters) {// code block to be executed
}Example 2 const numbers = [2,4,6,8];
for (const x of numbers) {// code block to be executed
}You can use a for...of loop to iterate over the elements of a Set:
const letters = new Set(["a","b","c"]);
for (const x of letters) {// code block to be executed
}You can use a for...of loop to iterate over the elements of a Map:
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
for (const x of fruits) {// code block to be executed
}The iterator protocol defines how to produce a sequence of values from an object. An object becomes an iterator when it implements a next() method.
method must return an object with two properties:
value (the next value)
done (true or false)value
(Can be omitted if done is true) done true if the iterator has completed false if the iterator has produced a new value Technically, iterables must implement the Symbol.iterator method. In JavaScript the following are iterables:
Symbol.iterator method:
This iterable returns never ending number: 10,20,30,40,.... when the next() method is called: