DEV Community

Cover image for ES2022 Features
Dhanush N
Dhanush N

Posted on

ES2022 Features

Javascript keeps on evolving and the new syntaxes has been followed in ES2022. Some of them are ๐Ÿ‘‡

1) Private Class fields

In olders ways we can make a class variable as private. We use the # to determine that it is private.

class Hello {
name = "Dhanush",
#work ="Engineer"
}

console.log(Hello.name) // Prints "Dhanush"
console.log(Hello.#work) // Cannot be read from outside

2) Top Level Await

We can do a await call directly in a javascript file, without defining an asynchronous function

await db.collection();

Previously

async function result(){
await db.collection();
}

await result()

Top level await works only when the script type is module

3) Accessing arrays

The at method for accessing array elements, which also helps to access elements from the last by providing negative indices

`const array = [1,2,3,4,5]

array.at(2) // 2
array.at(-1) // 5`

4) Object.hasOwn

Used to find whether the property exists in an object

Syntax: Object.hasOwn(<object>, <object_property>)

5) Regex Indices

New feature in regex which adds a d flag in regex expression

In the output you would get an array of indices, which contains the starting point to the ending point of where the keyword is found in the regular expression match.

Any inputs or additions feel free to add below ๐Ÿ‘‡

For more insights & tech lets stay connected via Twitter

Top comments (0)