It’s incredible to think that the LANDMARK Javascript release es6 was over 6 years ago (See the official release here)! Javascript and its community have learned a lot since that time, one point of criticism was how HUGE the es6 release was and how difficult it was to learn EVERYTHING. With that said, ES2021 adds a nice digestible set of awesome features coming to your browser soon 🙂 Here are my favorite things being added!

1. String.prototype.replaceAll()

I think this is a really slick feature, hopefully you think the same. Have you ever had to replace some characters in javascript with String.prototype.replace() only to find you had to have a global flag with a regex? Well not anymore, because now with String.prototype.replaceAll(), it’s very straightforward and much more readable.

Let’s go see its MDN entry:

The String.prototype.replaceAll() method returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match.


Example usage:

2. Numeric separators

Maybe I think this is cooler than it really is but I think this feature speaks for itself. I used to make a few Javascript games here and there and I feel this would have really helped me back then with some of the values I stored. I do feel though that the hex stuff I will start using today. Overall it’s SOOO much easier to read, I love it.

3. Logical OR assignment operators

This is a slick feature and to understand why it’s so cool is to compare it to the other ways of doing this. Granted ES6 brought us default parameters in functions which I feel this would be a good candidate for (MDN here) using it, but this still has a lot of value in general day-to-day usage of Javascript.

Also if you like this feature, check out the logical and operator (MDN here).

4. Promise.any()

This is an interesting feature for sure, so what’s a good use case for this? Borrowing from the MDN we get a good idea here…

“This method is useful for returning the first promise that fulfills. It short-circuits/stops after a promise fulfills, so it does not wait for the other promises to complete once it finds one. Unlike Promise.all(), which returns an array of fulfillment values, we only get one fulfillment value (assuming at least one promise fulfills. “

What’s a good example of using Promise.any()?

In the weekly SPS UI-guild we actually discussed this new feature (BTW always looking for new members, message Chris N about it!). One cool idea that fellow UI-guild member Ken Korth thought of was …

“…you were trying to get an accurate time from a time server and you were trying many time servers, when the first one came back, you could use any because the rest would be canceled…”

Or Imagine we are hitting an API endpoint and what comes back is relatively large/slow like a large MSTR sales report whose data changes infrequently. We could have a cached API and the live data API BOTH go out and whichever API comes back first is displayed to the user. Cool right?

What’s the difference between Promise.race() and this?

There is a very important KEY difference between the two, basically, Promise.any() vs Promise.race come down to how they handle failing Promises.

  • Promise.any() – Brings back the first promise, and only one result. Shows failure when all promises fail.
  • Promise.race() – Brings back the first promise that succeeds or fails.

And there are even more features coming in the release, notably weakrefs (MDN here). Check out the v8 release notes here for even more info.

Hopefully, you can start to use these features in your daily workflow, and before you know it we’ll have es2022 features. Happy coding!

Leave a Reply