- The original address: Everything about the latest ECMAScript release | ECMAScript 2021
- Kritika Sharma
- The Nuggets translation Project
- Permanent link to this article: github.com/xitu/gold-m…
- Translator: Hoarfroster
- Proofread by: KimYang, tong-h
In this article, we’ll walk you through the latest version of ECMAScript 2021 with some sample code.
The new function
1. String.replaceAll( )
Replace all instances of the target string found with the desired string:
const fact = "JavaScript is the best web scripting language. JavaScript can be used for both front end and backend";
console.log(fact.replaceAll("JavaScript"."TypeScript"));
/ / output:
// "TypeScript is the best web scripting language. TypeScript can be used for both front end and backend";
Copy the code
Compare this to the previous replace() method, which replaces only the first match of the target string with the desired string:
const fact = "JavaScript is the best web scripting language. JavaScript can be used for both front end and backend";
console.log(fact.replace("JavaScript"."TypeScript"));
/ / output:
// "TypeScript is the best web scripting language. JavaScript can be used for both front end and backend";
Copy the code
2. Promise.any( )
As soon as any of the supplied promises are resolved, promise.any () is resolved directly, while promise.all () is marked as resolved until all promises are resolved, basically the opposite of promise.all ().
If “Keeping a Promise” :
const promises = [
Promise.reject('A' wrong),
Promise.reject(Error 'B'),
Promise.resolve('results')];Promise
.any(promises)
.then((result) = > assert.equal(result, 'results')); //true
Copy the code
If “All promises are rejected” :
const promises = [
Promise.reject('A' wrong),
Promise.reject(Error 'B'),
Promise.reject('error C')];Promise
.any(promises)
.catch((aggregateError) = > {
assert.deepEqual(aggregateError.errors,
['A' wrong.Error 'B'.'error C']); //true
});
Copy the code
3. Logical assignment operators
A | | = b is equivalent to a | | (a = b) (short-circuit operator)
Why not a = a | | b?
Well, because for the previous expression, the assignment is only performed if A evaluates to false. Therefore, the former is assigned only when necessary. In contrast, the latter expression always performs assignment.
An example of a | | = b:
var a = 1;
var b = 2;
a ||= b;
console.log(a); / / 1
Copy the code
An example of a &&= b:
var a = 1;
var b = 2;
a &&= b;
console.log(a); / / 2
Copy the code
a ?? An example of = b:
var a;
var b = 2; a ?? = b;console.log(a); / / 2
Copy the code
4. Numeric delimiters
We can now use an underscore (_) as a separator between numeric and bigInt text. This will help developers improve the readability of their numeric text (the “underscore” will basically act as the “comma” we use to write numbers in everyday life (to provide separation between groups of numbers).
let budget = 1000000000000; // You can write:
let budget = 1 _000_000_000_000;
console.log(budget); // Print normal numbers:
/ / output:
/ / 1000000000000
Copy the code
Hopefully, this article has helped you keep up with the latest version of ECMAScript. Thank you for reading and feel free to comment if you have any questions.
References:
- Dev. To/faithfuloje…
- 2 ality.com/2020/09/ecm…
If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.