ES2022 introduced the at() method for strings.
Retrieve the third character from the name string.
const name = “W3Schools”; let letter = name.at(2); |
Access the third letter of the name string.
const name = “W3Schools”; let letter = name[2]; |
The at() method returns an element at a specified index from a string.
It behaves the same as using traditional array indexing ([]).
The at() method has been supported in all modern browsers since March 2022.
ES2022 introduced the /d
modifier to indicate the start and end of a match.
let text = “aaaabb”; let result = text.match(/(aa)(bb)/d); |
RegExp modifiers are used to specify options such as case-insensitivity and to enable other types of global searches.
Modifier |
Description |
i |
Perform case-insensitive matching |
g |
Perform a global match (find all) |
m |
Perform multiline matching |
d |
Perform substring matches (New in ES2022) |
ES2022 introduces a safer way to check if a property is an own property of an object.
Object.hasOwn() works similarly to Object.prototype.hasOwnProperty, but it supports all object types.
Object.hasOwn(“initProp”) |
ES2022 allows you to specify the cause of an error using the error.cause
property.
try { connectData(); } catch (err) { throw new Error(“Connecting failed.”, { cause: err }); } |
JavaScript modules can now delay execution until resources that require import are loaded.
import {myData} from ‘./myData.js’; const data = await myData(); |
class Hello { counter = 0; // Class field } const myClass = new Hello(); let x = myClass.counter; |
Class field declarations have been supported in all modern browsers since April 2021.
class Hello { #counter = 0; // Private field #myMethod() {} // Private method } const myClass = new Hello(); let x = myClass.#counter; // Error myClass.#myMethod(); // Error |
Private methods and fields have been supported in all modern browsers since June 2021.