Iterables are objects that can be iterated over (such as Arrays).
They can be accessed using straightforward and efficient code.
You can use for…of loops to iterate over iterables.
The JavaScript for…of statement iterates over the elements of an iterable object.
for (variable of iterable) { // code block to be executed } |
Iterating is straightforward; it refers to looping through a sequence of elements.
Here are some simple examples:
You can utilize a for…of loop to iterate through the characters 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 through the elements of an array.
const letters = [“a”,“b”,“c”]; for (const x of letters) { // code block to be executed } |
const numbers = [2,4,6,8]; for (const x of numbers) { // code block to be executed } |