ECMAScript 2022

JavaScript version number

Old ECMAScript versions are named by numbers: ES5 and ES6.

Starting from 2016, versions are named by year: ES2016, 2018, 2020, 2022.

New features of ES2022

Warning:

These features are relatively new.

Old browsers may require alternative code (Polyfill).

JavaScript Array at()

ES2022 introduced array methods at():

Example 1

Get the third element of the 'fruits' array:

const fruits = ['Banana', 'Orange', 'Apple', 'Mango'];
let fruit = fruits.at(2);

Try it yourself

Example 2

Get the third element of the 'fruits' array:

const fruits = ['Banana', 'Orange', 'Apple', 'Mango'];
let fruit = fruits[2];

Try it yourself

at() This method returns the element at the specified index from the array.

at() Method is similar to [] The returned results are the same.

Starting from March 2022, all modern browsers support at() Method:

Chrome Edge Firefox Safari Opera
Chrome 92 Edge 92 Firefox 90 Safari 15.4 Opera 78
April 2021 July 2021 July 2021 March 2022 August 2021

Note:

Many languages allow the use of negative indices (such as [-1]) Access the end element of an object/array/string.

This is not possible in JavaScript because [] Used to access arrays and objects. obj[-1] refers to the value of the key -1, not the last property of the object.

at() This method was introduced in ES2022 to solve this problem.

JavaScript String at()

ES2022 introduced string methods at():

Example 1

Get the third letter of the 'name' string:

const name = "W3Schools";
let letter = name.at(2);

Try it yourself

Example 2

Get the third letter of the 'name' string:

const name = "W3Schools";
let letter = name[2];

Try it yourself

at() Method returns the character at the specified index from the string.

at() Method is similar to [] The returned results are the same.

Starting from March 2022, all modern browsers support at() Method:

Chrome Edge Firefox Safari Opera
Chrome 92 Edge 92 Firefox 90 Safari 15.4 Opera 78
April 2021 July 2021 July 2021 March 2022 August 2021

RegExp d modifier

ES2022 adds /d Modifiers, used to indicate the start and end of a match.

Instance

let text = "aaaabb";
let result = text.match(/(aa)(bb)/d);

Try it yourself

RegExp modifiers are used to specify case-insensitivity and other global searches:

Modifier Description Try it
g Execute global matching (find all). Try it
i Execute case-insensitive matching. Try it
d Execute substring matching (added in ES2022). Try it
m Execute multi-line matching. Try it

Object.hasOwn()

ES2022 provides a safe method to check if a property is an own property of an object.

Object.hasOwn() Similar to Object.prototype.hasOwnPropertybut supports all object types.

Instance

Object.hasOwn(myObject, age)

Try it yourself

Error Cause

ES2022 allows specifying the root cause of an error through error.cause.

Instance

connectData(); 
  catch (err) { 
} 
  throw new Error("Connecting failed.", { cause: err }); 
}

Try it yourself

JavaScript await import

JavaScript modules can now wait for required resources before execution:

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;

Starting from April 2021, class field declarations are supported in all modern browsers:

Chrome Edge Firefox Safari Opera
Chrome 72 Edge 79 Firefox 69 Safari 14.1 Opera 60
January 2019 January 2020 September 2019 April 2021 January 2020

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

Starting from June 2021, private methods and fields are supported in all modern browsers:

Chrome Edge Firefox Safari Opera
Chrome 74 Edge 79 Firefox 90 Safari 14.1 Opera 62
April 2019 January 2020 June 2021 April 2021 June 2019