New Features in ECMAScript 2021 Update
Last Updated :
02 Mar, 2023
ECMAScript is a part of JavaScript language which is mostly used in web technology, building websites, or web apps. ECMAScript is growing as one of the world's most widely used general-purpose programming languages. It is majorly used in embedding with web browsers and is also adopted for server and embedded applications.
New updates to ECMAScript will release this July. The new improvements are introduced to make JavaScript more powerful and also make working easy for developers. It provides new functions, simple ways to do complex work, and much more.
New updates: The new JavaScript features in ECMAScript 2021 are as follows.
1. Logical assignment Operators: Logical assignment operators introduce new operators which combine logical operators and assignment expressions.
And & Equals (&&=): It assigns when the value is truthy.
Previous version:
let x = 1;
if(x){
a = 10;
}
Output:
x = 10
New version:
let x = 1;
x &&= 10;
Output:
x = 10
OR & Equals (||=): It assigns when the value is falsy. The assignment operation happens only if x is a falsy value. If x contains 1 which is a truthy value, an assignment does not happen. Here x contains 0 therefore assignment happens.
Previous version:
JavaScript
let x = 0;
x = x || 10;
console.log(x);
Output:
x = 10
New version:
JavaScript
let x = 0;
x ||= 10;
console.log(x);
Output:
x = 10
Nullish coalescing & Equals (??=): Symbol ?? is a nullish coalescing operator in JavaScript. It checks if a value is null or undefined.
JavaScript
let x;
let y = 10;
x ??= y;
console.log(x);
console.log(y);
Output: The value of x is undefined, therefore the right-hand side expression is evaluated and sets x to 10.
10
10
2. Numeric Separators: To improve readability and to separate groups of digits, numeric literals use underscores as separators. It also can be used for Binary, Hex, and Octal bases.
JavaScript
// A billion dollar that I want to earn
const money = 1_000_000_000;
console.log(money);
Output:
1000000000
3. JavaScript String replaceAll() Method: If we want to replace all instances of a substring in string then this new method replaceAll() is very useful.
JavaScript
const s = "You are reading JavaScript 2021 new updates.";
console.log(s.replaceAll("JavaScript", "ECMAScript"));
Output:
You are reading ECMAScript 2021 new updates.
4. Promise.any() Method: It returns a promise that will resolve as soon as one of the promises is resolved. It is the opposite of the Promise.all() method which waits for all promises to resolve before it resolves. What will happen when all the promises are rejected, The method will throw an AggregateError exception with the rejection reason. We have written the code inside a try-catch block.
JavaScript
const promiseOne = new Promise((resolve, reject) => {
setTimeout(() => reject(), 1000);
});
const promiseTwo = new Promise((resolve, reject) => {
setTimeout(() => reject(), 2000);
});
const promiseThree = new Promise((resolve, reject) => {
setTimeout(() => reject(), 3000);
});
try {
const first = Promise.any([
promiseOne, promiseTwo, promiseThree
]);
// If any of the promises was satisfied.
} catch (error) {
console.log(error);
// AggregateError: If all promises were rejected
}
Output:
Uncaught (in promise) AggregateError: All promises were rejected
5. Private class methods: Private methods have scope inside the class only, so outside the class, they are not accessible.
Previous version:
JavaScript
class GfG {
showMe() {
console.log("I am a geek")
}
#notShowMe() {
console.log("Hidden informations")
}
}
const gfg = new GfG()
gfg.showMe()
gfg.notShowMe()
Output:
Explanation: The error is shown as follows. This is because notShowMe() is now a private method inside the class GfG and can only be accessed via a public method inside the class
New version:
JavaScript
class GfG {
showMe() {
console.log("I am a geek");
}
#notShowMe() {
console.log("Hidden informations");
}
showAll() {
this.showMe()
this.#notShowMe();
}
}
const gfg = new GfG();
gfg.showAll();
Output:
I am a geek
Hidden informations
Explanation: We create a new public method called showAll() inside the class GfG. From this public method, we can access the private method #notShowMe() and since our new method is public we get the following result.
6. Private Getters and Setters: Just like privatemethods, now we can make getters and setters so that they can only be accessed inside a class or by instance created.
JavaScript
class GfG {
get #Name() {
return "GeeksforGeeks"
}
get viewName() {
return this.#Name
}
}
let name = new GfG();
console.log(name.viewName);
Output:
GeeksforGeeks
Similar Reads
JavaScript ES5 (JS 2009) JavaScript 2009 (ES5) refers to the fifth edition of the ECMAScript language specification, standardized in 2009. It introduced several features, like strict mode, new methods, JSON support, and improved syntax for better programming practices and compatibility across browsers. ECMAScript 5 (ES5) in
7 min read
ES2015: Latest Version of JavaScript ES2015 is the latest version of JavaScript programming language. It is the first major upgrade to JavaScript since 1997. It was approved in June 2015 by ECMA international, an association responsible for approving ECMA standards which programming languages like JavaScript, CoffeeScript and TypeScrip
4 min read
JS 2015 or ECMAScript 6 (ES6) JS 2015 (ES6) also known as ECMAScript 6 (ES6), ECMAScript 6 (ES6) is a significant update to JavaScript, introducing arrow functions, classes, template literals, let and const for variable declaration, enhanced object literals, destructuring, and more modern features for better code organization an
10 min read
JS 2016 or ECMAScript 2016 JavaScript 2016 (ES2016) is a modified version of ES2015 in which they introduced some new features like JavaScript Exponentiation (**) operator, JavaScript Exponentiation assignment (**=), and Array..includes() method for array element presence checking, enhancing calculations, and array operations
2 min read
JS 2017 - ECMAScript 2017 JavaScript (JS) 2017, or ECMAScript 2017, introduced some new features in JavaScript. It enhanced asynchronous programming with async functions, provided shared memory and atomics for improved concurrency, and introduced Object.values() and Object.entries() for streamlined object manipulation. These
3 min read
JS 2018 - ECMAScript 2018 JavaScript 2018 (ES9) or ECMAScript 2018 is a modified version of ES8, in which ES9 introduced new features like asynchronous iteration, rest/spread properties, and enhancements to regular expressions, further improving asynchronous programming, object manipulation, and string handling capabilities.
4 min read
JS 2019 - ECMAScript 2019 ECMAScript 2019, also known as ES10, introduced features like Array.flat(), Array.flatMap(), Object.fromEntries(), and Symbol. description, and some string methods, for enhancing JavaScript's capabilities and expressiveness. JavaScript 2019 (ES10) or ECMAScript 2019 new features are: Name Descriptio
5 min read
JS 2020 - ECMAScript 2020 JavaScript ECMAScript 2020 (ES11) introduced new features like optional chaining, nullish coalescing, dynamic import(), BigInt, and Promise.allSettled(), etc. enhancing language capabilities for modern web development needs. JavaScript 2020 (ES11) or ECMAScript 2020 new features are: BigInttype for
5 min read
ECMAScript 2021 (JS 2021/2022) JavaScript in 2021/2022 continues to evolve, with ES2021/ES2022 bringing enhanced features like private class fields, promise improvements, and record data structures, boosting developer productivity and code quality. JavaScript New Features in ES2021Name Description Promise any(): Resolves if any P
4 min read
New Features in ECMAScript 2021 Update ECMAScript is a part of JavaScript language which is mostly used in web technology, building websites, or web apps. ECMAScript is growing as one of the world's most widely used general-purpose programming languages. It is majorly used in embedding with web browsers and is also adopted for server and
4 min read