bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/JavaScript/Debugging, Projects, and Reference
JavaScript•Debugging, Projects, and Reference

ECMAScript 2022

New Features in JavaScript 2022

Supported in all modern browsers since March 2023 .

FeatureDescription
Array at()Returns an indexed element from an array
String at()Returns an indexed element from an string
RegExp /dPerform substring matches
Object.hasOwn()Check if a property is the own property of an object
error.causeLets you specify the reason behind an error
await importLets JavasSript modules wait for resources that require import before running.
Class field declarationsAllows for properties to be defined directly within a class
Private methods and fieldsAllows for private properties (#method and #field)

Browser Support

ECMAScript 2022 is supported in all modern browsers since March 2023 :

Chrome 94Edge 94Firefox 93Safari 16.4Opera 80
Sep 2021Sep 2021Oct 2021Mar 2023Oct 2021

JavaScript Array at()

Examples

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

ES2022 intoduced the array method at()

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:

Many languages allows negative bracket indexing like [-1] to access elements from the end of an object / array / string.

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

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

JavaScript String at()

Examples

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

ES2022 intoduced the string method at()

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

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

RegExp Modifiers are used to spescfy case-insensitive, and other global searches:

ModifierDescription
iPerform case-insensitive matching
gPerform a global match (find all)
mPerform multiline matching
dPerform substring matches (New in ES2022)

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.

Object.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)

try {
  // Create a JSON parse failure JSON.parse("blablablabla");
} catch(err) {
let text = err.name + " " + err.message;
}

Example (now)

try {
  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:

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

Previous

Project - Form Validation

Next

ECMAScript 2021

This chapter

Overview
32

Lessons

180m

Read time

1. JavaScript Errors2. JavaScript Debugging3. JavaScript Statements4. Project - localStorage Counter5. ECMAScript 20266. JavaScript Silent Errors7. JavaScript Debugging Console8. JavaScript Reserved Words9. Project - Event Listener10. ECMAScript 202511. JavaScript Error Statements12. JavaScript Debugging Breakpoints13. JavaScript Operator Precedence14. Project - To-Do List15. ECMAScript 202416. JavaScript Debugging Errors17. Project - Modal Popup18. ECMAScript 202319. JavaScript Debugging Async20. Project - Form Validation21. ECMAScript 202222. ECMAScript 202123. ECMAScript 202024. ECMAScript 201925. ECMAScript 201826. ECMAScript 201727. ECMAScript 201628. Javascript 2015 (ES6)29. JavaScript 2009 (ES5)30. JavaScript 1999 (ES3)31. JavaScript Versions32. JavaScript History

On this page

New Features in JavaScript 2022Browser SupportJavaScript Array at()JavaScript String at()RegExp d ModifierObject hasOwnError CauseJavaScript await importJavaScript Class Field DeclarationsJavaScript Private Methods and Fields