ECMAScript 2021 is the latest JavaScript version that introduced multiple new features to the JavaScript language.
In this blog, we will cover
replaceAll()
method and numeric separators
that was a part of
ECMAScript 2021
additions.
String.prototype.replaceAll()
String.prototype.replaceAll()
method
replaces all instances
of a substring in a string
with a new string value.
Syntax
String.prototype.replaceAll(subString, newSubstring)
Parameters
subString: Input string that is to be replaced by a new value. It can be a string or a regular expression.
newSubstring: The string that replaces the substring argument
Return value
The original string is left unchanged and a new string is returned.
Before
Before ECMAScript 2021 one of the most common ways of doing this was to use a global regexp.
Now, this approach works fine but using regular expression is not easy, they are also not easy to read and error-prone.
After
Starting with ECMAScript 2021 we have a straightforward way to solve this problem.
Now let us take a look at the numeric separator.
Numeric Separator
Numeric separator(_) allows users to make their numeric literals more readable by creating a the visual separation between groups of digits.
Before
After
Position of Separator
A numeric separator can appear anywhere inside the number.
These are some of the valid usages of numeric separators
However, numeric separator is not allowed at the starting or at the end of the number.
The numeric separator can be applied to number, float, binary, hex, octal and bigint literal types.
Following are some of the valid examples for different literal types.