15 07 Es2021
15 07 Es2021
ECMAScript 2021
Key Value
Name ECMAScript 2021
Short name ES2021
Was finalized in June 2021
Feature list
# Name Desc
1 Logical Assignment Operators ??= , ||= , &&=
//"Or Or Equals"
x ||= y;
x || (x = y);
// "QQ Equals"
x ??= y;
x ?? (x = y);
1/4
Javascript cho người mới bắt đầu 2021 🎉 Easy Frontend © 2021
let a = 1;
let b = 0;
a &&= 2;
console.log(a);
// expected output: 2
b &&= 2;
console.log(b);
// expected output: 0
const a = { duration: 50 };
2. Numeric separator
// before
const count = 1000000;
3. String.prototype.replaceAll()
// Before: need to use regex with global flag
const queryString = 'q=query+string+parameters';
const withSpaces = queryString.replace(/\+/g, ' ');
// 'q=query string parameters'
// ES2021
const queryString = 'q=query+string+parameters';
// const withSpaces = queryString.split('+').join(' ');
queryString.replaceAll('+', ' ');
// 'q=query string parameters'
3/4
Javascript cho người mới bắt đầu 2021 🎉 Easy Frontend © 2021
4. Promise.any
Promise.any([
fetch('https://v8.dev/').then(() => 'home'),
fetch('https://v8.dev/blog').then(() => 'blog'),
fetch('https://v8.dev/docs').then(() => 'docs')
]).then((first) => {
// Any of the promises was fulfilled.
console.log(first);
// → 'home'
}).catch((error) => {
// All of the promises were rejected.
console.log(error);
});
4/4