Home

Posts

Resume

ES2021, What's new?

Fri, 29 Jan 2021


In this post, we'll take a look at the most interesting features that should be included in ES12, the new version of the standard.

The new feature that will be included in ES12, aka ES2021, the JavaScript specification developed by ECMA for 2021, are currently under discussion, and a few weeks ago, on October 15th, a new draft was released on TC39 website. You can also check active proposals either here or on GitHub.

in this post we will briefly glance through some new features coming in JavaScript ES12 (ES2021). You can also check out all proposals here.

String.prototype.replaceAll

This is useful for replacing every occurence of a given string and returns a new string without altering the original variable.

const string1 = "this is a string containing, string word in a string"

const string2 = string1.("string", "replaced");

The prior solution was the replace method, which only replaced the initial occurence

const string1 = "replacing a string with a string"

const string2 = string1.replace("string", "replaced"); // result: replacing replaced with a string"

To replace all with the replace method, you will need to use RegEX
const string3 = string1.replace(/string/g, "replaced"); // result: replacing replaced with a replaced"

Promise.any

This is behaves similarly to Promise.race, but they have different rejection format. if all the promises in Promise.any fails, its throws an AggregateError contaning the reason for the rejection of all the promises.

Promise.any(promises).then(result => {
	console.log('failure reason:'. result)
})

Numeric Seperators

This is useful is helping you separate numeric values with underscore while avoiding errors. I think this will improve the cleanliness and readability of you code. With two simple rule:

  • Rule 1: The numeric value cannot start with an underscore.
  • Rule 2: The numeric value cannot end with an underscore.
const number = 1_000_000; // returns: 1000000
const number2 = 1_00_000; // returns: 100000
//This isn't restricted to integers only
const number3 = 1.234_456; // returns: 1.23456

const number4 = _100_000; //SyntaxError
const number5 = 100_000_; //ReferenceError

Logical Operators and Assignment Expressions

With ES12, you can combine Logical operators (&&, || and ??) with assignment expression (=).

a ||= b; // returns a = a || b, this returns a if a is truthy or b when a is falsy

c &&= d; // returns c = c && d, this returns d if c and d are truthy or c otherwise

e ??= f; // returns e = e ?? f, returns f if e is undefined or null else returns e

Of course, this aren't all the proposed updates, but these are some I'm looking forward to. if there are any updates you're also looking forward to, feel free to share.