JS: Iterator.prototype.drop

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2025)

iterator.drop(n)
  • Remove the first n yield of iterator.
  • Return a new Generator.
// define a generator function
function* gf() {
 for (let x of [1, 2, 3, 4, 5]) yield x;
}

// create a generator. a generator is both iterable and iterator
const xgen = gf();

const xresult = xgen.drop(3);

console.assert(
 JSON.stringify(
  Array.from(xresult),
 ) === `[4,5]`,
);

// xresult is iterable
console.assert(
 Reflect.has(xresult, Symbol.iterator),
);

// xresult is iterator
console.assert(
 Reflect.has(xresult, "next"),
);