ECMAScript 2021 (ES12) new features

Sameer Dewan
3 min readFeb 22, 2021

Our annual ECMAScript dialect update is on the way with an expected release of June 2021.

So, what’s new?

So in ECMAScript 2021, what is the difference between String.replace() and String.replaceAll()? String.replace() will only replace the first instance of the found targeted item to replace, whereas String.replaceAll() will apply changes to all instances of the targeted item. Nifty.

Example of replaceAll vs replace

We’re starting to get to a place with class-based Javascript programming that resembles the functionality other languages offer us. Private methods and accessors are a necessity in class-based programming. The syntax to how to approach this in ECMAScript 2021 is as follows.

Using private method and accessors in ES2021

We should utilize WeakRefs for large amounts of data cache or large object that need to be garbage collected and not prevented in doing so by references. In the below example, we can see the in 5 seconds when the element the WeakRef references is removed, the CountDown instance is stopped.

An example of using WeakRefs

Finalizers work with WeakRefs to register callback functions that are fired when the object is garbaged collected.

An example of using a Finalizer

Promise.any() helps us grab the first resolved promise if any are accepted, or throws an AggregateError exception if all throw errors.

Using Promise.any()

We’re all used to now (probably) using logical operators such as && and || in our code. We can now use these operators in value assignment in ECMAScript 2021.

We can conditionally assign values depending on conditions being met!

Conclusion

In my opinion, these features will be greatly appreciated by the community. I for one, can’t wait to start using private class fields and Promise.any()!

All the best.

--

--