ECMAScript 2020, also known as ES11, introduced several new features to JavaScript. Some of the key features introduced in ECMAScript 2020 include:
-
BigInt:
- Allows the representation of arbitrarily large integers.
-
Created by appending
n
to the end of an integer literal or by calling theBigInt()
constructor. -
Example:
const bigNum = 123456789012345678901234567890n;
-
Dynamic Import:
-
Enables importing modules dynamically using
import()
. - Allows loading modules conditionally or on-demand.
-
Example:
const module = await import('./module.js');
-
Enables importing modules dynamically using
-
Nullish Coalescing Operator (??):
- Provides a way to handle default values for null or undefined values.
-
Returns the right-hand side operand when the left-hand side operand is
null
orundefined
. -
Example:
const value = null ?? 'default';
-
Optional Chaining (?.):
-
Simplifies accessing properties of nested objects when some properties may be
null
orundefined
. - Allows chaining of property accesses without having to check for each level's existence.
-
Example:
const address = user?.address?.city;
-
Simplifies accessing properties of nested objects when some properties may be
-
globalThis:
- Provides a standard way of accessing the global object across different environments.
- Offers a unified way to access the global object regardless of the context (e.g., browser, Node.js).
-
Example:
globalThis.console.log('Hello');
-
Promise.allSettled():
- Returns a promise that resolves after all the provided promises have settled (fulfilled or rejected).
- Provides an array of objects with information about each promise's fulfillment status.
-
Example:
Promise.allSettled([promise1, promise2]) .then(results => console.log(results));
These features enhance JavaScript by providing better ways to handle nullish values, manage asynchronous operations, and improve code readability and maintainability.