ECMAScript 2022


JavaScript Version Numbers

Previous versions of ECMAScript were identified by numbers, specifically ES5 and ES6.

Starting in 2016, we began naming versions based on the year of release: ES2016, 2018, 2020, and 2022.

New Features in ES2022


Warning

These attributes are quite recent.

Outdated web browsers might require a different code, commonly known as a Polyfill.


JavaScript Array at()

ES2022 brought in a new array method called at().

The at() function retrieves a specific element from an array based on its index.

The at() function gives the same result as using [].

Note

Most languages let you use negative bracket indexing, such as [-1], to retrieve elements from the end of an object, array, or string.

In JavaScript, using [] for accessing arrays and objects makes this task impossible. When obj[-1] is used, it points to the value of the key -1 and not to the last property of the object.

The at() method came about in ES2022 to address this issue.


JavaScript String at()

ES2022 added a new string function called at().

The at() function retrieves a specific element from a string based on its index.

The at() function provides the same result as using [].


RegExp d Modifier

ES2022 introduced the /d modifier to indicate the beginning and end of a match in a simpler way.

Regular expression modifiers are employed to indicate case-insensitive and other global searches.

Modifier Description
i Enable matching without considering capitalization.
g Find and match all occurrences globally.
m Match multiple lines of text.
d Find partial matches within a string (introduced in ES2022).

Object hasOwn

The Object.hasOwn() function is like Object.prototype.hasOwnProperty, but it works with all types of objects.


Error Cause

ES2022 allows you to indicate the cause of an error using the error.cause.


JavaScript await import

JavaScript modules can now pause and wait for necessary resources to be imported before executing.

import {myData} from './myData.js';

const data = await myData();

JavaScript Class Field Declarations

class Hello {
  counter = 0; // Class field
}
const myClass = new Hello();

let x = myClass.counter;

JavaScript Private Methods and Fields

class Hello {
  #counter = 0;  // Private field
  #myMethod() {} // Private method
}
const myClass = new Hello();

let x = myClass.#counter; // Error
myClass.#myMethod();      // Error