Новые функции JavaScript ES2020

Nullish Coalescing Operator
The Nullish Coalescing Operator (??) is a new feature in ES2020 that provides a way to assign a default value to a variable if the current value is null or undefined, but not for falsy values like empty strings or zeros.
This operator is especially useful when working with configurations, function arguments, or any situation where you want to differentiate between a variable being explicitly set to null/undefined or being set to a falsy value.
- Nullish Coalescing Operator
- Optional Chaining
- BigInt
Optional Chaining
Optional Chaining (?.) is another powerful addition to JavaScript in ES2020. It allows you to safely access deeply nested properties of an object without worrying about causing an error if an intermediate property is null or undefined.
This feature simplifies the process of checking for the existence of nested properties before accessing them, making your code more concise and readable.
BigInt
The introduction of BigInt in ES2020 provides native support for arbitrarily large integers in JavaScript. This data type allows you to work with integers beyond the safe integer limit imposed by the Number type.
BigInts can be used for applications that require handling large numbers, such as cryptography, financial calculations, or any scenario where precision and range are crucial.
Dynamic Import
The Dynamic Import feature in ES2020 allows you to import modules dynamically at runtime. This means you can conditionally load modules based on certain criteria, improving the performance and flexibility of your applications.
Dynamic Import is particularly beneficial when working with large applications where loading all modules upfront may not be efficient. It enables you to load modules only when they are needed, optimizing resource utilization.
«With great power comes great responsibility.» — Uncle Ben
GlobalThis
The GlobalThis property introduced in ES2020 provides a standard way to access the global object across different environments. It offers a consistent way to interact with the global scope, regardless of whether your code runs in a browser, Node.js, or other environments.
GlobalThis simplifies cross-environment development by offering a reliable mechanism to access global objects, reducing the need for environment-specific code and enhancing code portability.
Promise.allSettled
The Promise.allSettled method in ES2020 is a valuable addition to working with promises. Unlike Promise.all which rejects immediately if any promise rejects, Promise.allSettled waits for all promises to settle (either resolve or reject) before returning the results.
This feature is useful when you need to execute multiple asynchronous operations and want to handle all outcomes, regardless of whether they succeed or fail. It provides a more robust way to work with promises in complex scenarios.
String.prototype.matchAll
The String.prototype.matchAll method introduced in ES2020 enhances the capabilities of working with regular expressions in JavaScript. It returns an iterator of all results matching a specified pattern in a string, allowing you to iterate over multiple matches.
This method is beneficial when you need to extract multiple occurrences of a pattern from a string and perform operations on each match individually. It provides a convenient way to work with regular expressions in a more versatile manner.
