JS Iterables
In this page:
for (const item of iterable) {
// item is each element
}
const iterator = iterable[Symbol.iterator]();
What Makes Something Iterable
A value is considered iterable if it implements a specific internal method, accessible through Symbol.iterator, that defines how to step through its values one at a time.
उदाहरण: What Makes Something Iterable
const arr = [1, 2, 3];
console.log(typeof arr[Symbol.iterator]); // "function", arr is iterable
for...of With Strings
Strings are iterable, meaning for...of can step through a string one character at a time, correctly handling multi-character symbols better than some older indexing approaches.
उदाहरण: for...of With Strings
// Declare the constant `word`, set to "Hi"
// Declare the constant `word`, set to "Hi"
const word = "Hi";
// Loop over `word`, binding each item to `char`
// Loop over `word`, binding each item to `char`
for (let char of word) {
// Print `char` to the console
// Print `char` to the console
console.log(char);
}
for...of With Arrays
Arrays are the most common iterable used with for...of, providing direct access to each element's value without requiring manual index tracking.
उदाहरण: for...of With Arrays
// Declare the constant `nums`, set to `[10, 20, 30]`
// Declare the constant `nums`, set to `[10, 20, 30]`
const nums = [10, 20, 30];
// Loop over `nums`, binding each item to `n`
// Loop over `nums`, binding each item to `n`
for (let n of nums) {
// Print `n` to the console
// Print `n` to the console
console.log(n);
}
for...of With Maps and Sets
Both Map and Set, covered in more detail in later chapters, are built-in iterables, letting for...of naturally step through a Set's values or a Map's key-value pairs.
उदाहरण: for...of With Maps and Sets
// Declare the constant `set` as a new `Set` instance
// Declare the constant `set` as a new `Set` instance
const set = new Set([1, 2]);
for (let v of set) console.log(v);
// Declare the constant `map` as a new `Map` instance
// Declare the constant `map` as a new `Map` instance
const map = new Map([["a", 1]]);
for (let [k, v] of map) console.log(k, v);
Why Iterables Matter
The shared iteration protocol is what lets a single, consistent for...of syntax work across many completely different data structures, giving JavaScript a unified way to process collections without learning a separate looping style for each type.
उदाहरण: Why Iterables Matter
// Define the function `sum` taking `iterable`
// Define the function `sum` taking `iterable`
function sum(iterable) {
// Declare the variable `total`, set to `0`
// Declare the variable `total`, set to `0`
let total = 0;
for (let n of iterable) total += n;
// Return `total` from this function
// Return `total` from this function
return total;
}
// Print `sum([1, 2, 3])` to the console
// Print `sum([1, 2, 3])` to the console
console.log(sum([1, 2, 3]));
// Print `sum(new Set([4, 5]))` to the console
// Print `sum(new Set([4, 5]))` to the console
console.log(sum(new Set([4, 5])));
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic