New features in JavaScript ES2021 (ES12) that have been approved by the Ecma General Assembly
About a year ago, I wrote an article about the new features that JavaScript would incorporate in its ES2021 specification.
Today, in June 2021, these features, along with others, have been approved.
In this article, I elaborate and detail what I wrote more than a year ago, but with the addition of the final features that have been approved.
String.prototype.replaceAll()
replace all instances of a substring in a string with another string value, without using a global regexp.
Until now, the most common way of doing this was to use a global regexp.
Consider the following code where we use a regular expression to replace the “+” character with an empty character:
const fullname= 'fullname=Jhon+Hannibal+Smith';
const fullnameFormated = fullname.replace(/\+/g, ' ');
//output: Jhon Hannibal Smith
This approach has the downside of requiring a regular expression that uses special characters to achieve this. But the use of regular expressions is not easy and is error-prone and a common source of bugs.
#javascript #programming #coding #es2021