ES2022 (ES13) | Most wanted features | Very interesting
I could see many interesting features which ES2022 would provide. Lets see all those important features and how we can utilize those into our day-2-day coding —
Declaration of Class Level Fields
Earlier we could only declare a field inside constructor of a Class but having ES2022, we can declare a field inside a Class without having a constructor.
Class can have private fields and methods
Earlier there was no way to declare a private field in a Class which can’t be accessed outside of the Class. With ES2022, you can declare a private field or a private method in a Class.
We just need to put # before a field to make it private.
Example (private field)
Example (private method)
arr to access the value at index 1 of an
Array but you cannot do
arr[-1] to count backward from the ending of the
Array. The reason is that the brackets syntax is used not only for arrays but also for Objects, where
obj[-1] would simply refer to the property ‘-1’ of that
With the .
at() method we now have an easy way to access any index, positive or negative of arrays and strings:
Instead of writing:
We could now be able to write like this-
Top-level Await Operator
The modules can now wait for resources, requiring other modules that import them to do so before performing their code.
jQuery = await import('https://cdn-a.com/jQuery');
Above code will make sure importing jquery first before executing the next line.
Ergonomic brand checks for private fields using the "
An attempt to access a private field outside of a class currently results in an exception being thrown, rather than
undefined being returned as we already seen above in this article.
We could use
catch inside of a class to determine if a private field exists. However, the issue with this method is that it can be difficult to determine the source of an exception.
ES2022 will provide developers with the
in keyword that can be used to produce a Boolean indicating the presence of a private field. This simple solution does not require a
catch method or exceptions:
#name = 'Suneet';
#name = 'Anna';
return #name in obj;
I hope this article would help you to understand the most interesting features of ES2022!!!
Stay tuned and subscribe my medium channel!!!