Overview of ES2020 New JavaScript Features
This article summarizes the major ES2020 JavaScript enhancements—including Promise.allSettled, globalThis, optional chaining, nullish coalescing, import.meta, BigInt, dynamic import, private class fields, matchAll, deterministic for‑in order, and module namespace exports—providing concise explanations and code examples for each feature.
ES2020 introduced several new JavaScript features that improve asynchronous handling, global object access, optional chaining, nullish coalescing, module metadata, big integers, dynamic imports, private class fields, matchAll, deterministic for‑in order, and module namespace exports.
#1 Promise.allSettled returns a promise that resolves after all input promises settle, providing an array of objects with status , value (if fulfilled) or reason (if rejected). Example:
const sleep = (timeout) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("sleep finish!");
}, timeout);
});
};
Promise.allSettled([sleep(2000), Promise.reject(new Error("error"))])
.then(res => {
console.log("res", res);
});#2 globalThis offers a standard way to access the global object across environments, e.g., globalThis.location === window.location; // true .
#3 Optional Chaining (?.) safely accesses deep properties without explicit checks. Example:
const adventurer = { name: 'Alice', cat: { name: 'Dinah' } };
console.log(adventurer.dog?.name); // undefined#4 Nullish Coalescing (??) returns the right‑hand operand when the left is null or undefined , otherwise the left operand.
#5 import.meta provides module metadata; in browsers it contains the module URL. Example:
console.log(import.meta); // {url: "http://127.0.0.1:8099/html/2020-JavaScript/index.js"}#6 BigInt enables representation of integers larger than 2^53‑1 . Literal syntax appends n , e.g., 9007199254740991n . BigInt is not strictly equal to Number but loosely equal.
#7 Dynamic Import allows modules to be loaded on demand, returning a promise. Example:
if (flag) {
import('./module.js').then(fn => {
fn.say();
});
}#8 Private Class Variables use # prefix for private fields, methods, and static members, which are only accessible within the class body.
class Cat {
#name = 'tom';
getName() {
console.log(this.#name);
}
}#9 String.prototype.matchAll returns an iterator of all matches, including capture groups. Example:
let regexp = /t(e)(st(\d?))/g;
let str = 'test1test2';
let array = [...str.matchAll(regexp)];
console.log(array[0]); // ["test1", "e", "st1", "1"]#10 for…in order is now standardized in ES2020, guaranteeing a deterministic property enumeration order.
#11 Module namespace exports allow re‑exporting all exports under a single name, e.g., export * as utils from './module.js'; .
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.