JS TutorialLesson #142

ECMAScript 2021

ECMAScript 2021 is supported in all modern browsers since April 2021:

New Features in JavaScript 2021

FeatureDescription
Promise.any()Takes promises as input and returns a single promise
String replaceAll()Replaces all occurrences of a substring in a string
Numeric SeparatorNew numeric separator (_) makes numbers more readable

Browser Support

ECMAScript 2021 is supported in all modern browsers since April 2021:

Chrome 85Edge 85Firefox 79Safari 14.1Opera 71
Aug 2020Aug 2020Jul 2020Apr 2021Sep 2020

JavaScript Promise.any()

Promise.any() is a static method in JavaScript that takes an iterable of Promises as input and returns a single Promise.

Example

Example Code

javascript
Try it Yourself
// 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);

});

JavaScript String ReplaceAll()

ES2021 introduced the string method replaceAll():

Example

Example Code

javascript
Try it Yourself
text = text.replaceAll("Cats","Dogs");

text = text.replaceAll("cats","dogs");

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

Example Code

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

text = text.replaceAll(/cats/g,"dogs");
ES2020 introduced the string method matchAll().

JavaScript Numeric Separator (_)

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

Example

Example Code

javascript
Try it Yourself
const num = 1_000_000_000;

The numeric separator is only for visual use.

Example

Example Code

javascript
Try it Yourself
const num1 = 1_000_000_000;

const num2 = 1000000000;

(num1 === num2);

The numeric separator can be placed anywhere in a number:

Example

Example Code

javascript
Try it Yourself
const num1 = 1_2_3_4_5;
The numeric separator is not allowed at the beginning or at the end of a number. > In JavaScript only variables can start with _.

Interactive Code Examples

JS Example 1

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
// 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);

});

JS Example 2

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
text = text.replaceAll("Cats","Dogs");

text = text.replaceAll("cats","dogs");

JS Example 3

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
text = text.replaceAll(/Cats/g,"Dogs");

text = text.replaceAll(/cats/g,"dogs");

JS Example 4

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const num = 1_000_000_000;

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const num1 = 1_000_000_000;

const num2 = 1000000000;

(num1 === num2);

JS Example 6

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const num1 = 1_2_3_4_5;