What's new in ES2021
What’s new in ES2021
Let’s get started with the first of the new ES2021 features:
String.prototype.replaceAll
String.replace
is a useful method that allows us to replace a pattern in a string with something else. The problem is that if we want to use a string
as a pattern and not a RegEx, only the first occurrence will get replaced.
Press + to interact
const str = "I like my dog, my dog loves me";const newStr = str.replace("dog", "cat");console.log(newStr);// "I like my cat, my dog loves me"
As the name implies, String.replaceAll
will do exactly what we need in this scenario, replace all the matching pattern, allowing us to easily replace all mentions of a substring, without the use of RegEx:
Press + to interact
const str = "I like my dog, my dog loves me";const newStr = str.replaceAll("dog", "cat");console.log(newStr);// "I like my cat, my cat loves me"
Promise.any
During the past years we’ve seen new methods such as Promise.all
and Promise.race
with ES6, Promise.allSettled
last year with ES2020 and ES2021 will introduce a new one: Promise.any
.
I bet you can already tell what ...
Access this course and 1400+ top-rated courses and projects.