JavaScript ES2020 Version (2020)
JavaScript ES2020 (ECMAScript 2020) is the eleventh major release of the JavaScript language specification. It was finalized in June 2020. ES2020 introduced several new features and enhancements to JavaScript, building upon the foundation laid by ES6 (ES2015). In this tutorial, we will learn about the new features introduced in ES2020 and how to use them in your JavaScript code.
Features of ES2020
ES2020 introduced several new features and enhancements to JavaScript. Some of the key features of ES2020 are:
-
BigInt: ES2020 introduced the
BigInt
primitive type, which allows you to represent arbitrarily large integers in JavaScript. TheBigInt
type is created by appendingn
to the end of an integer literal or by calling theBigInt()
constructor with a numeric value.For example:
BigInt Exampleconst bigNumber = 1234567890123456789012345678901234567890n;
console.log(bigNumber); // Output: 1234567890123456789012345678901234567890n -
Dynamic Import: ES2020 introduced dynamic
import()
expressions, which allow you to dynamically import modules in JavaScript. Theimport()
function returns aPromise
that resolves to the module namespace object of the requested module.For example:
Dynamic Import Exampleconst modulePath = './module.js';
import(modulePath)
.then((module) => {
console.log(module.default);
})
.catch((error) => {
console.error(error);
}); -
Nullish Coalescing Operator (
??
): ES2020 introduced the nullish coalescing operator (??
), which allows you to provide a default value for variables that arenull
orundefined
. The nullish coalescing operator returns the right-hand operand when the left-hand operand isnull
orundefined
, and otherwise returns the left-hand operand.For example:
Nullish Coalescing Operator Exampleconst name = null;
const defaultName = name ?? 'John Doe';
console.log(defaultName); // Output: 'John Doe' -
Optional Chaining Operator (
?.
): ES2020 introduced the optional chaining operator (?.
), which allows you to safely access nested properties of an object without having to check if each property exists. If a property isnull
orundefined
, the optional chaining operator short-circuits and returnsundefined
.For example:
Optional Chaining Operator Exampleconst user = {
name: 'Alice',
address: {
city: 'New York',
},
};
console.log(user.address?.city); // Output: 'New York'
console.log(user.address?.country); // Output: undefined -
Promise.allSettled(): ES2020 introduced the
Promise.allSettled()
method, which returns aPromise
that resolves after all the inputPromises
have settled (either fulfilled or rejected). ThePromise
returned byPromise.allSettled()
resolves with an array of objects that describe the outcome of eachPromise
.For example:
Promise.allSettled() Exampleconst promises = [
Promise.resolve(1),
Promise.reject(new Error('Error!')),
Promise.resolve(3),
];
Promise.allSettled(promises)
.then((results) => {
results.forEach((result) => {
console.log(result.status, result.value);
});
});
Browser Support for ES2020
Most modern web browsers support ES2020 features, but some older browsers may not fully support all ES2020 features. To ensure that your JavaScript code works across all browsers, you can use a transpiler like Babel to convert your ES2020 code into ES5 code, which is compatible with older browsers.
ES2020 features are generally well-supported in modern web browsers, and you can start using them in your JavaScript code without the need for a transpiler in most cases.
For more information on browser support for ES2020 features, you can check the table.
85+ | 79+ | 14+ | 79+ | 71+ | No | |||||||
Latest ✅ | Latest ✅ | Latest ✅ | Latest ✅ | Latest ✅ | Latest ❌ |
-
Chrome: 85+ (Latest ✅)
Chrome has full support for ES2020 features. You can use ES2020 features in Chrome without any issues. Chrome 85 and later versions fully support ES2020 features. Chrome is the most popular web browser, and it is recommended to test your JavaScript code in Chrome to ensure compatibility.
-
Firefox: 79+ (Latest ✅)
Firefox has full support for ES2020 features. You can use ES2020 features in Firefox without any issues. Firefox 79 and later versions fully support ES2020 features. Firefox is a popular web browser, and it is recommended to test your JavaScript code in Firefox to ensure compatibility.
-
Safari: 14+ (Latest ✅)
Safari has full support for ES2020 features. You can use ES2020 features in Safari without any issues. Safari 14 and later versions fully support ES2020 features. Safari is a popular web browser on macOS and iOS devices, and it is recommended to test your JavaScript code in Safari to ensure compatibility.
-
Edge: 79+ (Latest ✅)
Edge has full support for ES2020 features. You can use ES2020 features in Edge without any issues. Edge 79 and later versions fully support ES2020 features. Edge is a popular web browser, and it is recommended to test your JavaScript code in Edge to ensure compatibility.
-
Opera: 71+ (Latest ✅)
Opera has full support for ES2020 features. You can use ES2020 features in Opera without any issues. Opera 71 and later versions fully support ES2020 features. Opera is a popular web browser, and it is recommended to test your JavaScript code in Opera to ensure compatibility.
-
Internet Explorer: No support (Latest ❌)
Internet Explorer does not support ES2020 features. If you need to support Internet Explorer, you can use a transpiler like Babel to convert your ES2020 code into ES5 code, which is compatible with Internet Explorer.
Conclusion
ES2020 introduced several new features and enhancements to JavaScript, such as the BigInt
primitive type, dynamic import()
expressions, the nullish coalescing operator (??
), the optional chaining operator (?.
), and the Promise.allSettled()
method. These features improve the expressiveness and flexibility of JavaScript, making it easier to write clean and concise code.