What are ES2020 - ES11 features?

1. BigInt

BigInt is a new primitive type introduced to handle numbers larger than Number.MAX_SAFE_INTEGER (2^53 - 1). It allows safe operations on large integers.

const bigInt1 = 123456789012345678901234567890n;
const bigInt2 = BigInt("123456789012345678901234567890");

console.log(bigInt1 + bigInt2); // 246913578024691357802469135780n
console.log(typeof bigInt1); // "bigint"

2. Dynamic import()

Dynamic import() allows importing JavaScript modules dynamically at runtime instead of at the start of the script. It returns a promise that resolves to the module.

async function loadModule() {
const { default: moduleFunction } = await import('./module.js');
moduleFunction();
}

loadModule();

3. Nullish Coalescing Operator (??)

The nullish coalescing operator (??) provides a default value only when the left-hand operand is null or undefined. It differs from the || operator, which also considers falsy values like 0, false, or ''.

const name = null;
const defaultName = name ?? "Guest";
console.log(defaultName); // "Guest"

const count = 0;
console.log(count ?? 10); // 0 (unlike `||`, which would return 10)

4. Optional Chaining (?.)

The optional chaining operator (?.) simplifies accessing deeply nested properties without having to check if each level exists. If a reference is null or undefined, it short-circuits and returns undefined instead of throwing an error.

5. Promise.allSettled()

Promise.allSettled() takes an array of promises and returns a promise that resolves when all input promises settle (either fulfilled or rejected). It returns an array of objects describing the outcomes.

6. globalThis

globalThis provides a standard way to access the global object across environments (browser, Node.js, etc.). It eliminates the need for environment-specific global object names like window, global, or self.

This makes code more portable across platforms.

7. String.prototype.matchAll()

The matchAll() method returns an iterator with all matches of a regular expression, including capture groups, providing more detailed results compared to match().

8. for-in Order Guarantee

In ES11, the for-in loop guarantees that object properties are iterated in the order they were defined (for string keys). Previously, this behavior was implementation-dependent.

9. Import.meta

The import.meta object provides metadata about the current module. It is commonly used in JavaScript module environments to access specific information, like the module URL.

console.log(import.meta.url);
// Outputs the URL of the current module in environments like browsers or Node.js.

10. Improved Handling of Optional Catch Binding

Optional catch binding was improved to allow omitting the error parameter in try...catch statements if it's not needed.