JavaScript · ES2025

ES16 Interactive Hub — ECMAScript 2025

Iterator Helpers (lazy map/filter/take chains), Set methods (union/intersection/...), Promise.try, RegExp.escape, Import Attributes (with { type: "json" }), Float16Array — a richer functional toolbox + native FP16 for ML/GPU.

Experience
0 XP
Course completion0%

Iterator Helpers

Core topic

Medium8 min

💡 Concept summary

Adds map/filter/take/drop/toArray/forEach/reduce methods directly onto Iterator.prototype — process lazy chains without converting to an Array first.

🤔 Why do you need this feature?

Before ES16, to use methods like .map() / .filter() on a generator result, you had to convert to an Array with [...gen] or Array.from(). This LOSES LAZINESS — it reads the entire stream into memory even when you only need the first few elements. Iterator Helpers let you call methods directly on the iterator while preserving lazy evaluation, so take(n) truly pulls only n elements.

Before (old style)Verbose / clunky
// Before ES16 — must convert to Array, losing laziness
function* infinite() {
    let i = 0;
    while (true) yield i++;
}

// Trying [...infinite()].map(x => x * 2).slice(0, 5) → DIES because it's infinite
// Must loop manually:
function take(iter, n) {
    const result = [];
    let count = 0;
    for (const v of iter) {
        if (count >= n) break;
        result.push(v);
        count++;
    }
    return result;
}

function mapIter(iter, fn) {
    return (function*() {
        for (const v of iter) yield fn(v);
    })();
}

const first5Doubled = take(mapIter(infinite(), x => x * 2), 5);
console.log(first5Doubled); // [0, 2, 4, 6, 8]
ES16 (Modern)Optimal & recommended
// ES16 — native Iterator Helpers, lazy chain
function* infinite() {
    let i = 0;
    while (true) yield i++;
}

const first5Doubled = infinite()
    .map(x => x * 2)
    .take(5)
    .toArray();
console.log(first5Doubled); // [0, 2, 4, 6, 8]

// Filter + drop + take chain
const result = infinite()
    .filter(x => x % 3 === 0)  // 0, 3, 6, 9, 12, 15...
    .drop(2)                    // drop the first 2 → 6, 9, 12...
    .take(3)                    // take 3 → 6, 9, 12
    .toArray();
console.log(result); // [6, 9, 12]

// forEach / reduce / some / every / find are also available
const sum = [1, 2, 3, 4, 5].values()
    .map(x => x * x)
    .reduce((a, b) => a + b, 0);
console.log(sum); // 55
Got it down? Try running real code or take the quiz!

ES16 Quick Reference

Quick syntax overview + copy snippets

Iterator Helpers

Adds map/filter/take/drop/toArray/forEach/reduce methods directly onto Iterator.prototype — process lazy chains without converting to an Array first.

function* infinite() {
    let i = 0;
    while (true) yield i++;
...
Set Methods (union/intersection/...)

Set.prototype gains 7 native set-operation methods: union, intersection, difference, symmetricDifference, isSubsetOf, isSupersetOf, isDisjointFrom.

const a = new Set([1, 2, 3, 4]);
const b = new Set([3, 4, 5, 6]);
console.log([...a.union(b)]);              // [1,2,3,4,5,6]
...
Promise.try()

Wrap a function (sync or async) into a uniform Promise — catching both synchronous throws and async rejections in the same .catch().

function maybe(input) {
    if (input < 0) throw new Error("Sync error!");
    if (input === 0) return Promise.reject(new Error("Async error!"));
...
RegExp.escape()

A static function that escapes regex special characters in a string (e.g. . * + ? ( ) [ ] { } \ ^ $ |) — build safe regexes from user input without worrying about injection.

const userInput = "1.5+ years (2 projects)";
const safe = RegExp.escape(userInput);
console.log(safe); // the fully escaped string
...
Import Attributes & JSON Modules

The syntax `import x from "..." with { type: "json" }` standardizes importing non-JS files (JSON, CSS, WASM...) along with metadata so the host understands the format.

import config from './config.json' with { type: 'json' };
console.log(config.appName, config.version);
const data = await import('./data.json', { with: { type: 'json' } });
...
Float16Array

A 16-bit floating-point typed array (half-precision IEEE 754) — saves 50% memory compared to Float32, serving ML/GPU/Web Neural Network/sensor data.

const arr = new Float16Array([1.5, 2.25, 3.125]);
console.log(arr.byteLength); // 6 bytes (3 × 2) — 50% savings!
console.log(arr.length);    // 3
...