DEV Community

Cover image for ES2022 Features
Dhanush N
Dhanush N

Posted on

3

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

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay