ES12 - ECMAScript 2021

String.prototype.replaceAll()

The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match.

const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';

console.log(p.replaceAll('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the monkey reacted, was it really lazy?"


// global flag required when calling replaceAll with regex
const regex = /Dog/ig;
console.log(p.replaceAll(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the ferret reacted, was it really lazy?"

Promise.any()

Promise.any() takes an iterable of Promise objects and, as soon as one of the promises in the iterable fulfills, returns a single promise that resolves with the value from that promise. If no promises in the iterable fulfill (if all of the given promises are rejected), then the returned promise is rejected with an AggregateError, a new subclass of Error that groups together individual errors. Essentially, this method is the opposite of Promise.all().

const promise1 = Promise.reject(0);
const promise2 = new Promise((resolve) => setTimeout(resolve, 100, 'quick'));
const promise3 = new Promise((resolve) => setTimeout(resolve, 500, 'slow'));

const promises = [promise1, promise2, promise3];

Promise.any(promises).then((value) => console.log(value));

// expected output: "quick"

WeakRef

A WeakRef object lets you hold a weak reference to another object, without preventing that object from getting garbage-collected.

More information,

Logical Assignment Operators

The logical nullish assignment (x ??= y) operator only assigns if x is nullish (null or undefined).

Ex: 1

Ex: 2

Logical AND assignment (&&=)

The logical AND assignment (x &&= y) operator only assigns if x is truthy.

Logical OR assignment (||=)

The logical OR assignment (x ||= y) operator only assigns if x is falsy.

Numeric Separators

This feature enables developers to make their numeric literals more readable by creating a visual separation between groups of digits.

Regular Number Literals

Binary Literals

Hex Literal

BigInt Literal

Numeric Separators are also available within BigInt literals.

It can also be used similarly to Number literals

Numeric Separators are only allowed between digits of BigInt literals, and not immediately before the BigInt n suffix.

Octal Literal

While there isn't much of a benefit, numeric separators are available in the Octal Literal productions out of conventially being generally available in non-legacy productions. In other words, the intent for feature is to be broad available in non-legacy numeric literal types.

Last updated

Was this helpful?