Sitemap

ES2022 (ECMAScript 2022) Features

2 min readMay 28, 2024

--

  1. Top-Level await:

In the old days of JavaScript, you couldn’t use the `await` keyword outside of special asynchronous functions. If you tried, it would cause an error. But now, you can use `await` anywhere, even outside those special functions. This helps fix problems where different parts of your code need to wait for each other before they can do their thing. So, now modules can patiently wait for the things they need from other modules before they start working.

Here ill explain with one example for better understanding

  1. Create a module file (callFetchdata.js):

This file will contain the code to fetch data from an API.

2. Create the main module file (main.js):

Get Sneharani’s stories in your inbox

Join Medium for free to get updates from this writer.

This file will import the CallFetchdata function from callFetchData.js and use top-level await to fetch and log the data.

2. Class Fields and Private Methods:

Previously, you had to invoke a constructor to declare a class field. But now, you can declare the class field directly within the class body without the need to call the constructor.

Class Fields :

With this syntax, you no longer need to explicitly set class fields inside the constructor. They can be initialized directly within the class body.

Private Methods & Fields :

You can declare private fields and methods in a class by prefixing them with #.

I created a Counter class with a private field to keep track of the count and a private method to log the count. and also have public methods to increment and get the current count.

Press enter or click to view image in full size

--

--