Engine

Language examples.

These examples stay inside the executable 171/171 preview corpus. They demonstrate supported slices, not the entirety of ES2022.

Closures and recursion

function factorial(n) {
  if (n <= 1) return 1;
  return n * factorial(n - 1);
}

const makeCounter = start => () => ++start;
const next = makeCounter(40);
[factorial(5), next(), next()];

Objects, prototypes and receivers

const proto = {
  describe() { return this.name + ":" + this.score; }
};

function Player(name, score) {
  this.name = name;
  this.score = score;
}
Player.prototype = proto;

const player = new Player("Ada", 42);
player.describe();

Arrays and callbacks

const values = [1, 2, 3, 4];
const doubled = values.map(value => value * 2);
const total = doubled.reduce((sum, value) => sum + value, 0);
total;

Descriptors and accessors

const meter = { _value: 20 };
Object.defineProperty(meter, "value", {
  enumerable: true,
  get() { return this._value; },
  set(next) { this._value = Number(next); }
});
meter.value = "42";
Object.keys(meter);

Conversions and exceptions

const answer = {
  valueOf() { return 40; }
};

try {
  if (answer + 2 !== 42) throw new Error("bad answer");
  "ok";
} catch (error) {
  error.toString();
} finally {
  // normal, return and throw completions all cross cleanup regions
}

Current exclusions

Promises and jobs, modules, proxies, symbols, typed arrays, weak references, regular-expression objects, dates, JSON, Math breadth, Intl and browser/Node APIs are not part of this preview contract.