ECMAScript 2021
JavaScript version number
Old JS versions are named by numbers: ES5 (2009) and ES6 (2015).
Starting from 2016, versions are named by year: ECMAScript 2016, 2017, 2018, 2019, ...
New features
New features in ES2021:
- Promise.any()
- String method replaceAll()
- Numeric separators (_)
New features in ES2022:
- Array method at()
- String method at()
- Regular expression /d
- Object.hasOwn()
- error.cause
- await import
- Private methods and fields
- Class field declarations
Warning
These features are relatively new.
Older browsers may require alternative code (Polyfill)
JavaScript string method ReplaceAll()
ES2021 introduced the string method replaceAll():
Example
text = text.replaceAll("Cats","Dogs"); text = text.replaceAll("cats","dogs");
The replaceAll() method allows you to specify a regular expression instead of a string to be replaced.
If the parameter is a regular expression, the global flag must be set (g
Otherwise, a TypeError will be thrown.
Example
text = text.replaceAll(/Cats/g,"Dogs"); text = text.replaceAll(/cats/g,"dogs");
Tip:ES2020 The string method matchAll() was introduced.
JavaScript Numeric Separators (_)
ES2021 introduced numeric separators (_
) to make numbers more readable:
Example
const num = 1_000_000_000;
Numeric separators are for visual use only.
Example
const num1 = 1_000_000_000; const num2 = 1000000000; (num1 === num2);
Numeric separators can be placed at any position within a number:
Example
const num1 = 1_2_3_4_5;
Note
Numeric separators are not allowed at the beginning or end of a number.
In JavaScript, onlyVariablesThey can start with an underscore (_).
Since January 2020, all modern browsers have supported numeric separators:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 75 | Edge 79 | Firefox 74 | Safari 13.1 | Opera 67 |
June 2019 | January 2020 | October 2019 | September 2019 | June 2019 |