ecmascript21

What’s new with JavaScript in 2022 ?

JavaScript is a very popular development language. In this article, we will analyze why it is so popular. We will also talk about some new features in JavaScript following the new features introduced in the ECMAScript2021 standard. These new features allow to reduce the code size and to maximize its efficiency.

JavaScript’s increasing popularity

According to the study conducted by SlashData via its survey State of the Developer Nation, JavaScript is at the top of the ranking of the most used programming languages in the world. The latter has indeed gained more than 5 million followers in less than 3 years and now has more than 12.4 million users. JavaScript is ahead of Python with 9 million users, Java (8.2 million users), C/C++ (6.3 million users), PHP (6.1 million) and C# (6 million).

JavaScript can do virtually anything

JS is a very popular language because of its versatility. With it you can develop any part of the application development chain: Full Stack. You can use JS on any project on any platform. You can develop mobile applications via React Native, Capacitor or Cordova. The applications will have the native features of mobile devices (phone, tablets, etc.)

With ReactJS, VueJS or Angular frameworks, you can build quite advanced web applications. Most web pages on the net use JavaScript to make their content dynamic. Desktop applications are not left out with Electron. You can create software with it. With engines such as Unity, it is possible to create 2D or 3D video games with JavaScript. Famous software is developed in JavaScript, including Slack, Skype, Discord, Visual Studio Code, MailSpring, Atom. And finally, NodeJS is used for different scripts that can run on your PCs and servers.

JavaScript developer and the job market

JavaScript developers are in high demand. According to a study conducted by DevSkiller, nearly 70% of companies need a JavaScript developer. The latter can claim an annual income of more than 40K€ if he works in a company. He can also work as a freelancer with more flexible working hours.

What is the ECMAScript standard?

ECMAScript is a standard on which various programming languages such as JavaScript, ActionScript, JScript or TypeScript are based. It is also a language integrated into web browsers and used in application servers. The standard is standardized by the ECMA International organization. The first edition of the specifications was released in June 1997 and we are in the era of version 2021: ECMAScript21 which has just been approved.

What’s new in JavaScript

JavaScript developers should be aware of the new features supported by JavaScript following the specifications of the ECMAScript 2021 standard.
Let’s see some of these new features.

String.prototype.replaceAll()

ReplaceAll() now allows you to easily change a character or a string in a given document. This question has been asked many times on the forums for JavaScript developers for ages and JavaScript finally includes an easy solution.

Thus, instead of writing :

const test = “this-is-a-test!”;
test.replace(/-/g, “_”)

Or else

this-is-a-test!’.replace(/-/g, “_”)

You can directly write :

const test = “this-is-a-test!”;
test.replaceAll(“-“, “_”)

Or you can write

‘this-is-a-test!’.replaceAll(‘-‘, ‘_’);

Promise.any()

Promise already exists in JavaScript with 3 combinators: Promise.all(), Promise.race(), Promise.allSettled(). But ECMAScript21 introduces a last combiner: the Promise.any(). With the Promise.any() method, as soon as a promise is kept, it sends the value of the promise. If none of the promises are kept, the promise is broken with an AggregateError return, a new subclass of Error. Promise.any() does not behave like Promise.all(). The latter sends the values of all promises if and only if all promises in the iterable are fulfilled.

As an example, this code will send the value of promise2: “fast

const promise1 = Promise.reject(0);
const promise2 = new Promise((resolve) => setTimeout(resolve, 4000, ‘slow’));
const promise3 = new Promise((resolve) => setTimeout(resolve, 1000, ‘fast’));
const promises = [promise1, promise2, promise3];

Promise.any(promises).then((value) => console.log(value));

With Promise.all, the following code will display the values of the 3 promises after 6 seconds of waiting (after all the promises are kept)

const promise1 = new Promise((resolve) => setTimeout(resolve, 6000, ‘very slow’));
const promise2 = new Promise((resolve) => setTimeout(resolve, 4000, ‘slow’));
const promise3 = new Promise((resolve) => setTimeout(resolve, 1000, ‘fast’));
const promises = [promise1, promise2, promise3];

Promise.all(promises).then((value) => console.log(value));

The result would be :

(3)[“very slow”,”slow”,”fast”]

WeakRefs and Finalizers

Two new constructors have been introduced in the WeakRefs proposal. They are WeakRef and FinalizationRegistry. WeakRef or Weak References is used if we don’t want to keep an object in memory for a long time. So, if the program does not refer to the object, the memory will be freed. As for FinalizationRegistry, it is used to make callbacks once the object in memory has been collected.

Logical affection operators

You can combine the logical operators &&, ||, ?? with the assignment operator ‘=’. Thus, we can write

var x = 1;
var y = 2;
x &&= y;

or

var x = 1;
var y = 2;
if(x) {
x = y
}

And directly,

var x = 1;
var y = 2;
x &&= y;

Which will display “2”.

Numeric separators

It consists in introducing the hyphen under 8 (_) as a separator at the level of numbers. It can therefore be used as a separator of thousands to facilitate the reading of large numbers. Thus, this code will display “1500000”:

var separator = 1_500_000;
console.log(separator);

All in all, JavaScript is a very popular language and is not going to disappear any time soon. It is easy to learn and master. Knowing that with it, we can program almost anything, the career opportunities are real.