Skip to main content

Command Palette

Search for a command to run...

ES2022 Features

Published
•2 min read
ES2022 Features
D

Experienced Consultant, Full Stack Developer, R&D Engineer who loves to solve puzzles & technology problems by code/

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

More from this blog

Dhanush N

56 posts

Experienced Consultant & Software Engineer with experience around building and scaling applications over various emerging technologies. I love solving puzzles & provide technological solutions by code