Example 1 — Demo
Core idea.
JavaScript
const arr = [10, 20];
const it = arr[Symbol.iterator]();
console.log(it.next().value);
console.log([...arr].join(","));How It Works
- Symbol.iterator. Arrays implement it.
- next/spread. Consume values.
Symbol.iterator is a well-known symbol. Marks the default iterator method used by for...of and spread.
const arr = [10, 20];
const it = arr[Symbol.iterator]();
console.log(it.next().value);
console.log([...arr].join(","));Core idea.
const arr = [10, 20];
const it = arr[Symbol.iterator]();
console.log(it.next().value);
console.log([...arr].join(","));CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful