JavaScript 2022
JavaScript Basics

JavaScript 2022

ECMAScript 2022 Overview

The naming convention for ECMAScript versions initially used numbers like ES5 and ES6. Since 2016, versions have been named by year: ES2016, ES2018, ES2020, and most recently, ES2022.

New Features in ES2022

  • Array at() and String at(): Simplifies access to array and string elements by index.
  • RegExp /d Modifier: Introduces substring matches in regular expressions.
  • Object.hasOwn(): Provides a reliable way to check if an object has a particular property as its own.
  • error.cause: Allows attaching a cause to exceptions for better error handling.
  • await import: Supports dynamic imports with the await keyword for modules.
  • Class Field Declarations: Allows declaring class fields directly within class bodies.
  • Private Methods and Fields: Enables encapsulation of class members, restricting access to the class itself.

Code Examples and Browser Compatibility

Array and String at()

The at() method retrieves an element at a specified index, improving readability and supporting negative indices for reverse lookup:

const fruits = ["Banana", "Orange", "Apple", "Mango"];let fruit =; // "Apple"‍const name = "W3Schools";let letter =; // "3"

Supported in browsers like Chrome 92 and Firefox 90 from early 2022.

RegExp /d Modifier

Enhances regular expressions by marking the start and end of the match, useful in substring matching:

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


A safer alternative to Object.prototype.hasOwnProperty:

const obj = {prop: 1};
console.log(Object.hasOwn(obj, 'prop')); // true‍

Widely supported in modern browsers.


Facilitates better error handling by specifying the underlying cause of an error:

try {  
// Hypothetical function that might throw  connectData();} 
catch (err)
throw new Error("Connection failed", { cause: err });}‍

Implemented in browsers from around mid-2021.

await import

Allows dynamic module imports within async functions or modules:

async function loadData() 
const { myData } = await import('./myData.js'); 

This feature is now supported across most new browser versions.

JavaScript Class Field Declarations

Classes can now have their own fields without needing constructor code:

class Example {
counter = 0; // Public class field}‍
Private methods and fields ensure better data encapsulation:class Example 
{  #privateField = 0// Private field  #privateMethod()
{} // Private method}‍

Both features enhance class syntax and encapsulation, supported in browsers from early 2021.

These features, introduced in ECMAScript 2022, enrich JavaScript's functionality, allowing for more efficient, readable, and secure code. However, older browsers may require polyfills to support these new features.

Take a look into your desired course