JS: ECMAScript 2020

By Xah Lee. Date: . Last updated: .

GlobalThis

a new builtin variable globalThis whose value is the global object.

Nullish coalescing operator

Optional chaining operator

String.prototype.matchAll

easier way to get all matches and all captures.

before, you have to use String.prototype.match with Regex Flag g (global) . if you want all captures of all occurrences, you have to do a loop and rely on advancing Regex lastIndex and Regex Flag y (sticky) .

Big int

A new number primitive for working with arbitrary precision integers.

const x = 10n;
const y = 20n;
console.assert(x + y === 30n);
console.assert(typeof x === "bigint");

also new are typed array of big int:

Dynamic import, the import() syntax

New syntax: import *

import * as xyz from "./xyz.js";

New syntax: export *

xtodo

Dedicated export * as ns from 'module' syntax for use within modules.

Import.meta

xtodo

import.meta, a host- populated object available in Modules that may contain contextual information about the Module.

export {abc} from "./xyz.js";
export * from "./xyz.js";

Promise.allSettled

A new Promise combinator that does not short-circuit.

Order of properties of for-in loop

Increased standardization of for-in enumeration order.

in JS: ECMAScript 2020, what did it say for object property order

(ai answer)

In ES2020 (and later), object property order is standardized for the common enumeration methods (including those that were previously underspecified).

The order (for ordinary objects)

Defined via OrdinaryOwnPropertyKeys / [[OwnPropertyKeys]]:

  1. Array index properties first
    String keys that are canonical numeric strings representing integers in the range (0 \leq i < 2^{32}-1), in ascending numeric order.

  2. Other string properties next
    In chronological order of creation (insertion order).

  3. Symbol properties last
    In chronological order of creation.

What changed in ES2020

Before ES2020:

  • Methods like Reflect.ownKeys(), Object.getOwnPropertyNames(), Object.getOwnPropertySymbols(), and Object.defineProperties already followed the above order.
  • Common operations (Object.keys / values / entries, for...in, and sometimes JSON.stringify) were not required to follow it (legacy compatibility).

ES2020 (via the for-in mechanics proposal) aligned the rest of them with the same order. It removed the old language that left the order of EnumerableOwnPropertyNames / EnumerateObjectProperties flexible, so they now follow OrdinaryOwnPropertyKeys.

Caveats: Some edge cases (proxies, prototype changes or property deletions during iteration, certain exotic objects, etc.) are still not fully specified. Array-index keys are always sorted numerically first, regardless of insertion order.