ECMAScript 2021 (JS 2021/2022)
Last Updated :
25 Oct, 2023
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 ES2021
|
Resolves if any Promise resolves, else reject after all reject.
|
Replaces all occurrences of a substring with another in string
|
separating digits in numbers using underscores.
|
JavaScript in 2022 new features :
|
Retrieves element at index supports positive and negative indexing
|
Finds character at index, supports positive and negative indexing
|
Matches any digit (0-9) in a regular expression pattern.
|
Checks if object has its own specified property.
|
Accesses the cause of an error in the error object.
|
Awaits and imports a module dynamically in JavaScript code asynchronously.
|
Encapsulates data with restricted access using private methods and fields.
|
Defines class properties directly without constructor using concise syntax.
|
We will explore all the above methods along with their basic implementation with the help of examples.
Promise.any() is a JavaScript ES2021/ES2022 method that takes an array of Promises and returns the first resolved Promise, rejecting only if all Promises reject.
Syntax:
Promise.any(iter)
Example: This example, shows the basic use of Promise.any() method.
JavaScript
let prom1 = new Promise((resolve, reject) => {
reject("Failure");
})
let prom2 = new Promise((resolve, reject) => {
reject("Failed to load");
})
let prom3 = new Promise((resolve, reject) => {
resolve("Worked");
})
let prom4 = new Promise((resolve, reject) => {
resolve("Successful");
})
let prom = [prom1, prom2, prom3, prom4];
Promise.any(prom).then((val) => { console.log(val) });
String replaceAll() is a JavaScript method (ES2021/ES2022) replacing all occurrences of a substring with another, returning a new string.
Syntax:
const newString = originalString.replaceAll(regexp | substr , newSubstr | function)
Example: Below is an example of the String replaceAll() Method.
JavaScript
function gfg() {
let string = "Geeks or Geeks";
newString = string.replaceAll("or", "for");
console.log(newString);
}
gfg();
Method 3: Numeric Separators (_)
Numeric Separators (_) in JavaScript (ES2021/ES2022) allow improved readability by placing underscores as visual separators in large numbers for better code comprehension.
Syntax:
let num1 = 1_000_000_000;
Example: Here is the basic example of above method.
JavaScript
let num1 = 1_000_000_000;
let num2 = 1000000000;
console.log(num1 === num2);
The JavaScript Array at() method takes an integer value (index) as a parameter and returns the element of that index. It allows positive and negative integers. For the negative integer, it counts back from the last element in the array.
Syntax:
at(index);
Example: This code shows the value extracted at the given index.
JavaScript
const arr = [45, 32, 69, 21];
const index = 3;
console.log(arr.at(index));
String.at() finds character at specified index, supports positive and negative indexes. Positive indexes start from array start, negative from end.
Syntax:
let a = str.at(ind);
Example: In this example, we will use the String.at() method with a positive index.
JavaScript
let str1 = "Welcome to GeeksforGeeks";
let str2 = "GeeksforGeeks";
console.log(str1.at(3));
console.log(str2.at(8))
The RegExp \D Metacharacter in JavaScript is used to search non-digit characters i.e all the characters except digits. It is the same as [^0-9].
Syntax:
/\D/
Example: This example searches the non-digit characters in the whole string.
JavaScript
function geek() {
let str1 = "GeeksforGeeks@_123_$";
let regex4 = /\D/g;
let match4 = str1.match(regex4);
console.log("Found " + match4.length
+ " matches: " + match4);
}
geek()
OutputFound 17 matches: G,e,e,k,s,f,o,r,G,e,e,k,s,@,_,_,$
Object.hasOwn() is a method that checks if an object has a specific property directly on itself, returning true if found, else false.
Syntax:
Object.hasOwn(obj, prop)
Example: This example uses the hasOwn() method to check if a property exists or not.
JavaScript
let details = {
name: "Raj",
course: "DSA",
website: "geeksforgeeks.org",
}
console.log(Object.hasOwn(details, 'name'));
console.log(Object.hasOwn(details, 'course'));
console.log(Object.hasOwn(details, 'phone number'));
Supported browser:
- Chrome 1
- Edge 12
- Firefox 1
- Safari 1
- Opera 3
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