codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

JavaScript ES2021 Exciting Features

Taran
codeburst
Published in
3 min readOct 6, 2020

--

Every year, JavaScript update adds new features. This year ES2020 or ES11 was released (Read ES2020 features article), with ES2021 or ES12 expected to be released in mid-2021.

New features that are added to JavaScript each year go through a four-stage process, with the fourth stage being the last one. In this article, I will discuss the features that have already reached stage four and are added to the Google Chrome V8 engine.

List of the new features discussed in this article

  • String.prototype.replaceAll
  • Promise.any
  • Logical Operators and Assignment Expressions
  • Numeric Separators
  • Intl.ListFormat
  • dateStyle and timeStyle options for Intl.DateTimeFormat

String.prototype.replaceAll

In JavaScript, replace() method only replaces the first instance of a pattern in a string. If we want to replace all the matches of a pattern in a string, the only way to achieve that is by using global regexp.

The proposed method replaceAll() returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a regular expression, and the replacement can be a string or a function that gets executed for each match.

Promise.any

ES2021 will introduce Promise.any() method which short-circuits and returns a value, as soon as it hits the first resolved promise from the list/array of promises (as explained in Example 1a). If all the promises are rejected then it will throw an aggregated error message (as illustrated in Example 1b).

It is different from Promise.race() as that method short-circuits once one of the given promises either resolves or rejects.

Example 1a: Even though a promise is rejected earlier than a resolved promise, Promise.any() will return the first resolved promise.

--

--

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Taran

I am a Full stack developer. I enjoy learning and building new skills, and sharing what I’ve learned.

Responses (7)

Write a response