New Year, New JavaScript Features from ES2022

Top-level await. Private #. At() & more. What’s coming in ES2022.

JavaScript in Plain English
7 min readJan 9, 2022

--

Every year, the ES team gets many proposals to improve on the glorious JavaScript ecosystem and some of them eventually are implemented officially. In order to get accepted, a feature needs to go through 4 stages as mentioned in TC39 (also check out the TC39 repo).

The following sections are getting additions in the upcoming update:

  • For the awesome async/await fans we have: Module loading
  • For the savvy OOP lovers we have: Classes
  • For the marvelous functional programmers we have: Built-in Objects
ES2022 > ES2021

1. Module Loading

Tired of errors when awaiting at the root of a file? Upgrade incoming.

Everyone loves async functions and the await keyword as it saved us from the hell of nested promises but it limited the use of await to only async functions and not at the root of a file and as programmers we hate (or love) writing workarounds🤓.

In ES2022, we can use top-level await to import resources dynamically, this can prove useful in CLI scripts.

We can use it like this:

// load-resources.mjs 
// with top-level await
const data = await (await fetch("https://resources")).text();
export const resource = JSON.parse(data).resource;

It’s supported by 81% of devices and Node.js 14.8+ as of January 2022.

2. Classes

Private everything

As the saying goes keep your variables private but your relationship public.

Most OOP-oriented languages use private and public to limit or extend the visibility of fields, methods, or accessors. But JavaScript had no limits (or limitations) in this regard. Now, they are included in JavaScript by prefixing with #.

class LikeCounter {
#likes;
constructor(likes) {
this.#likes = likes;
}

--

--

I love writing and building software, kinda hope I’m funny too. Check out my portfolio here snowfox.art.