JavaScript ES2021 Version (2021)
JavaScript ES2021 (ECMAScript 2021) is the twelfth major release of the JavaScript language specification. It was finalized in June 2021. ES2021 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 ES2021 and how to use them in your JavaScript code.
Features of ES2021
ES2021 introduced several new features and enhancements to JavaScript. Some of the key features of ES2021 are:
-
String.prototype.replaceAll(): ES2021 introduced the
String.prototype.replaceAll()
method, which allows you to replace all occurrences of a substring within a string with another substring. ThereplaceAll()
method replaces all occurrences of a specified substring with another substring and returns a new string.For example:
String.prototype.replaceAll() Examplelet str = "Hello, World!";
let newStr = str.replaceAll("o", "0");
console.log(newStr); // Output: Hell0, W0rld! -
Logical Assignment Operators: ES2021 introduced the logical assignment operators
||=
(logical OR assignment) and&&=
(logical AND assignment). These operators allow you to assign a value to a variable only if the variable isnull
,undefined
,false
,0
,NaN
, or an empty string (""
).For example:
Logical Assignment Operators Examplelet x = 10;
x ||= 20; // Equivalent to: x = x || 20;
console.log(x); // Output: 10
let y = 0;
y &&= 5; // Equivalent to: y = y && 5;
console.log(y); // Output: 0 -
Numeric Separators: ES2021 introduced numeric separators, which allow you to use underscores (
_
) as separators in numeric literals to make them more readable. Numeric separators are ignored by the JavaScript engine and are used only for formatting purposes.For example:
Numeric Separators Examplelet billion = 1_000_000_000;
console.log(billion); // Output: 1000000000
let pi = 3.14159_26535;
console.log(pi); // Output: 3.1415926535 -
WeakRef: ES2021 introduced the
WeakRef
object, which allows you to create weak references to objects. Weak references do not prevent the garbage collector from collecting the object they reference. TheWeakRef
object is useful for implementing caches or other data structures that need to hold references to objects without preventing them from being garbage collected.For example:
WeakRef Examplelet obj = { name: "Alice" };
let weakRef = new WeakRef(obj);
console.log(weakRef.deref()); // Output: { name: 'Alice' } -
FinalizationRegistry: ES2021 introduced the
FinalizationRegistry
object, which allows you to register objects for cleanup when they are garbage collected. TheFinalizationRegistry
object is used to perform cleanup actions on objects before they are collected by the garbage collector.For example:
FinalizationRegistry Examplelet obj = { name: "Alice" };
let finalizationRegistry = new FinalizationRegistry((heldValue) => {
console.log(`Cleaning up: ${heldValue.name}`);
});
finalizationRegistry.register(obj, "held value");
obj = null; // Allow obj to be garbage collected -
Promise.any(): ES2021 introduced the
Promise.any()
method, which takes an iterable ofPromises
and returns a newPromise
that resolves as soon as one of the inputPromises
is fulfilled. If all the inputPromises
are rejected, thePromise
returned byPromise.any()
is rejected with anAggregateError
that contains an array of rejection reasons.For example:
Promise.any() Exampleconst promises = [
Promise.reject("Error 1"),
Promise.resolve("Success 1"),
Promise.reject("Error 2"),
];
Promise.any(promises)
.then((value) => {
console.log(value); // Output: "Success 1"
})
.catch((error) => {
console.error(error); // Output: AggregateError: All promises were rejected
}); -
AggregateError: ES2021 introduced the
AggregateError
object, which is used to represent multiple errors in a single error object. TheAggregateError
object contains an array of errors as itserrors
property.For example:
AggregateError Exampleconst error1 = new Error("Error 1");
const error2 = new Error("Error 2");
const aggregateError = new AggregateError([error1, error2]);
console.log(aggregateError.errors); // Output: [Error: Error 1, Error: Error 2]
Browser Support for ES2021
Most modern web browsers support ES2021 features, but some older browsers may not fully support all ES2021 features. To ensure that your JavaScript code works across all browsers, you can use a transpiler like Babel to convert your ES2021 code into ES5 code, which is compatible with older browsers.
ES2021 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. However, it is always a good practice to check the compatibility of the features you are using with the target browsers to ensure a consistent user experience.
For more information on browser support for ES2021 features, you can check the compatibility tables provided by Can I use.
Conclusion
In this tutorial, we learned about the ES2021 version of JavaScript (ECMAScript 2021) and the new features introduced in ES2021. We explored features such as String.prototype.replaceAll()
, logical assignment operators, numeric separators, WeakRef
, FinalizationRegistry
, Promise.any()
, and AggregateError
. You can start using these features in your JavaScript code to take advantage of the latest enhancements in the language.