HPAL TECH
Apr 28, 2022

What You Should Know About JavaScript ES2021

Introduction

JavaScript is a widely used computer language, particularly in web development. Every year, thanks to the ECMAScript versions, a slew of new features for JavaScript are introduced. ECMAScript 2021 offers a number of useful and intriguing features that we can use in JavaScript. We can look at them now because they haven't been released yet.

We'll look at some of the most useful ES2021 features in JavaScript in this article.

let myNumber = 1_000_000; console.log(myNumber); //output: 1000000 let num = 0.000_0002; console.log(num); //output: 2e-7

2. Replace All

One of the features of ES2021 that I really enjoy is the replaceAll() method. You can use this method to replace all of the characters in a string without using a regex. The replaceAll() method requires two parameters: the character to be replaced and the character to be replaced with.

The following example can assist you in better comprehending it.

let myStr = ‘Prograssing’; console.log(myStr.replaceAll(“s”, “m”)); //output: Programming

As you can see, we used replaceAll () to replace all the s characters with m ones (). This eliminates the need for regex in such circumstances and allows you to simply replace items on strings.

3. Promise Any

As a parameter, Promise.any() accepts an array of promises. Promise.any() will return the first resolved promise if all of the promises have been resolved. You will receive an error if all of the promises are denied.

Here's an illustration:

const promise1 = new Promise((resolve, reject) => { resolve(‘promise1 was resolved.’); }); const promise2 = new Promise((resolve, reject) => { resolve(‘promise2 was resolved.’); }); const promise3 = new Promise((resolve, reject) => { resolve(‘promise3 was resolved.’); });let result = Promise.any([promise1, promise2, promise3]); console.log(result); //output: promise1 was resolved.

Promise.any() returned promise1 because it was the first to be resolved, as seen above. If all of the promises were refused, we'd get an AggregateError with the rejection reasons.

4. Weak Reference

WeakRef is another amazing aspect of ES2021 (). It's used to keep a weak reference to another object, meaning it won't stop the trash collector from collecting it. It's useful when we don't want to remember a thing for a long time.

We send an object as an input to WeakRef () to build a new WeakRef, and then call deref() on the weak reference to read it.

Take a look at the following example:

const myObject = new WeakRef({ name: ‘John’, age: 25 });//Read the object. console.log(myObject.deref()); //output: {name: “John”, age: 25}//Access name. console.log(myObject.deref().name); //output: John

Hislordship

Hislordship

Hislordship is a young talented young man who seeks to explore more in this tech industry. Currently React and Django developer

Leave a Reply

Related Posts

Categories