function* naturals() {
let n = 1;
while (true) yield n++;
}
// Get first 5 even squaresconst result = naturals()
.map(n => n ** 2)
.filter(n => n % 2 === 0)
.take(5)
.toArray();
console.log(result); // [4, 16, 36, 64, 100]
Note ES2025. Chainable lazy operations on any iterator. Unlike array methods, these process elements one at a time, making them memory-efficient for large or infinite sequences.