ECMAScript 2022
Supported in all modern browsers since March 2023.
New Features in JavaScript 2022
Supported in all modern browsers since March 2023.
| Feature | Description | |
|---|---|---|
| Array at() | Returns an indexed element from an array | |
| String at() | Returns an indexed element from an string | |
| RegExp /d | Perform substring matches | |
| Object.hasOwn() | Check if a property is the own property of an object | |
| error.cause | Lets you specify the reason behind an error | |
| await import | Lets JavasSript modules wait for resources that require import before running. | |
| Class field declarations | Allows for properties to be defined directly within a class | |
| Private methods and fields | Allows for private properties (#method and #field) |
Browser Support
ECMAScript 2022 is supported in all modern browsers since March 2023:
| Chrome 94 | Edge 94 | Firefox 93 | Safari 16.4 | Opera 80 |
| Sep 2021 | Sep 2021 | Oct 2021 | Mar 2023 | Oct 2021 |
JavaScript Array at()
ES2022 intoduced the array method at():
Examples
Example Code
javascriptconst fruits = ["Banana", "Orange", "Apple", "Mango"];
let fruit = fruits.at(2);The at() method returns an indexed element from an array.
The at() method returns the same as [].
The at() method is supported in all modern browsers since March 2022:
JavaScript String at()
ES2022 intoduced the string method at():
Examples
Example Code
javascriptconst name = "EA Academy";
let letter = name.at(2);The at() method returns an indexed element from an string.
The at() method returns the same as [].
RegExp d Modifier
ES2022 added the /d modifier to express the start and end of the match.
Example
Example Code
javascriptlet text = "aaaabb";
let result = text.match(/(aa)(bb)/d);RegExp Modifiers are used to spescfy case-insensitive, and other global searches:
| Modifier | Description | Try it |
|---|---|---|
| i | Perform case-insensitive matching | Try it |
| g | Perform a global match (find all) | Try it |
| m | Perform multiline matching | Try it |
| d | Perform substring matches (New in ES2022) | Try it |
Object hasOwn
ES2022 provides a safe way to check if a property is the own property of an object.
Object.hasOwn() is similar to Object.hasOwnProperty() but supports all object types.
Example
Example Code
javascriptObject.hasOwn(myObject, age)Error Cause
Starting from ECMAScript 2022, the Error constructor (and its subclasses like TypeError, SyntaxError, etc.) supports an optional {cause} property.
This allows you to create chained errors - where one error includes another as its cause.
Example (before)
Example Code
javascripttry {
// Create a JSON parse failure
JSON.parse("blablablabla");
} catch(err) {
let text = err.name + " " + err.message;
}Example (now)
Example Code
javascripttry {
try {
// Create a JSON parse failure
JSON.parse("blablablabla");
} catch(err) {
// Create a new error and include the original one as the cause
throw new Error("Failed to load JSON", {cause:err});
}
} catch(err){
let text = err.name + " " + err.message;
document.getElementById("demo").innerHTML = text;
}JavaScript await import
JavasSript modules can now wait for resources that require import before running:
Example
Example Code
javascriptimport {myData} from './myData.js';
const data = await myData();JavaScript Class Field Declarations
Example
Example Code
javascriptclass Hello {
counter = 0; // Class field
}
const myClass = new Hello();
let x = myClass.counter;JavaScript Private Methods and Fields
Example
Example Code
javascriptclass Hello {
#counter = 0; // Private field
#myMethod() {} // Private method
}
const myClass = new Hello();
let x = myClass.#counter; // Error
myClass.#myMethod(); // ErrorInteractive Code Examples
Examples
javascriptconst fruits = ["Banana", "Orange", "Apple", "Mango"];
let fruit = fruits.at(2);Examples
javascriptconst name = "EA Academy";
let letter = name.at(2);JS Example 3
javascriptlet text = "aaaabb";
let result = text.match(/(aa)(bb)/d);JS Example 4
javascriptObject.hasOwn(myObject, age)Example (before)
javascripttry {
// Create a JSON parse failure
JSON.parse("blablablabla");
} catch(err) {
let text = err.name + " " + err.message;
}