ECMAScript 2021

JavaScript Version Numbers

Old ECMAScript versions was named by numbers: ES5 and ES6.

From 2016, versions are named by year: ES2016, 2018, 2020 ...

New Features in ES2021

Warning

These features are relatively new.

Older browsers may need an alternative code (Polyfill)

JavaScript Promise.any()

Example

// Create a Promise
const myPromise1 = new Promise((resolve, reject) => {
  setTimeout(resolve, 200, "King");
});

// Create another Promise
const myPromise2 = new Promise((resolve, reject) => {
  setTimeout(resolve, 100, "Queen");
});

// Run when any promise fulfill
Promise.any([myPromise1, myPromise2]).then((x) => {
  myDisplay(x);
});
Try it Yourself »

Promise.any() is supported in all modern browsers since September 2020:

Chrome 85 Edge 85 Firefox 79 Safari 14 Opera 71
Aug 2019 Aug 2020 Jul 2020 Sep 2020 Sep 2020

JavaScript String ReplaceAll()

ES2021 introduced the string method replaceAll():

Example

text = text.replaceAll("Cats","Dogs");
text = text.replaceAll("cats","dogs");
Try it Yourself »

The replaceAll() method allows you to specify a regular expression instead of a string to be replaced.

If the parameter is a regular expression, the global flag (g) must be set, otherwise a TypeError is thrown.

Example

text = text.replaceAll(/Cats/g,"Dogs");
text = text.replaceAll(/cats/g,"dogs");
Try it Yourself »

Note

ES2020 introduced the string method matchAll().



JavaScript Numeric Separator (_)

ES2021 intoduced the numeric separator (_) to make numbers more readable:

Example

const num = 1_000_000_000;
Try it Yourself »

The numeric separator is only for visual use.

Example

const num1 = 1_000_000_000;
const num2 = 1000000000;
(num1 === num2);
Try it Yourself »

The numeric separator can be placed anywhere in a number:

Example

const num1 = 1_2_3_4_5;
Try it Yourself »

Note

The numeric separator is not allowed at the beginning or at the end of a number.

In JavaScript only variables can start with _.

The numeric separator is supported in all modern browsers since January 2020:

Chrome 75 Edge 79 Firefox 74 Safari 13.1 Opera 67
Jun 2019 Jan 2020 Oct 2019 Sep 2019 Jun 2019


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.