Some outstanding features in JavaScript ES12 2021

Tram Ho

Some features stand out in javascript ES12

  1. Introduce
  2. Private Accessors
  3. Promise.any () and AggregateError
  4. Numeric Separators
  5. String.prototype.replaceAll ()
  6. Logical Assignment Operators
  7. Conclude

1. Introduction

  • ES12 2021 is in phase 4 and is expected to launch this year with many useful features.
  • It allows us to be easy to use, saves time, and makes the code a lot easier to read.
  • These outstanding features will be listed in the article below.

2. Private Accessors

  • Accessors functions can be made private by writing # before the function

For example:

  • get and set in the above code is the keyword that makes cat_name () an access attribute. Although cat_name () looks like a function, it can be read as an ordinary property like so:

3. Promise.any () and AggregateError

  • Promise.any () is called immediately after any promises are made or all of them are rejected.

  • Three concurrent requests are shown in the code above. Once one of the requests is resolved, Promise.any is also resolved and records the first resolved request in the console (dantri in the example above).
  • If all promises are rejected, Promise.any will throw a new exception of the error: AggregateError.
  • What’s new about it is that the AggregateError object represents an error where several errors are encapsulated in a single error.
  • It should look like this:

  • The above e.errors is an array of errors object.

4. Numeric Separators

  • When we work with large numbers it can be difficult or confusing. For example: “21989854”. Look closely to see that this is 21 million and …
  • With the new feature of ES2021, we can rewrite with underscores to make it easier to see as “21_989_854”. It was much easier to see by looking at it. Now, we can clearly see that’s 21 million.
  • Hence, the Numeric Separators feature is meant to simply improve readability. It does not affect the performance of our application.

  • Looking at the example above we see that it is much easier to read.

5. String.prototype.replaceAll ()

  • In ES12 replaceAll is simplified in usage than before, instead of using regex we have simpler syntax.

6. Logical Assignment Operators

  • With ES12, there is a combination of the logic operator and the assignment expression:

7. Conclusion

  • These are the new functions of ES12 I learned, you can refer to below:
Share the news now

Source : Viblo