ECMAScript 2021 (ES12) new features
Tags: es12, ecmascript2021, javascript, frontend
Published: 2021-09-03 10:08:37

---
title: ECMAScript 2021 (ES12) new features
published: true
description: 
tags: ES12,ECMAScript2021,javaScript,frontend, 
cover_image: https://media-exp1.licdn.com/dms/image/C4E12AQHiczyWfCVh-w/article-cover_image-shrink_720_1280/0/1629662599633?e=1635379200&v=beta&t=lpjRpfsOvmXX8cIWwFnxA73VjuYohys1ua-OLP9PlEg
---

ECMAScript, the standardized version of JavaScript is increasing its popularity and is becoming powerful every day. Since the introduction of ECMAScript 2015 (ES6) which was an immense growth forward, new features are added every year around June.  Those features are mostly improvements to the JavaScript language by providing new functions and by expressing things in much simpler way. It also changes the way developers structure the program. 

On June 22 The ECMA International approved the latest version of the official specification ES12 aka ECMAScript 2021 by providing capabilities for strings, promises, classes and much more. The following list show those new features of ES12

* Numeric separators 
* String replaceAll()
* Logical assignment operator
* Promise.any
* Private class methods
* Private getters and setters
* WeakRef

--- 

### Numeric separators 

Numeric separators are used for larger numbers that are hard to read by separating them with <code>(_)</code> between a group of digits.  Let see this by an example

![Night](https://media-exp1.licdn.com/dms/image/C4E12AQHBMM2INKpsiA/article-inline_image-shrink_1000_1488/0/1629918681182?e=1635379200&v=beta&t=8AAiaiD69nteaZXj-vuY3r12buRJWTeoBEV4CgTJWWU)

As we see from the example above it is hard to read the value if it is a million, ten million, or a billion. With the help of the numeric separators <code>(_)</code>, we can divide the same number in group of digits as in example below 

![Night](https://media-exp1.licdn.com/dms/image/C4E12AQGrFsjK0CXiDA/article-inline_image-shrink_1000_1488/0/1629918634829?e=1635379200&v=beta&t=I57fD-s2AEwoWyvfUw2Z64s7KBgborJR-htnWqNvtHs)

---
### String replaceAll()

I find this method as my favorit one to use further in my codebase when working with strings and substrings replacements as there is no need on using regular expressions.

![Night](https://media-exp1.licdn.com/dms/image/C4E12AQEMm3sNWJ_jEg/article-inline_image-shrink_1000_1488/0/1629919244553?e=1635379200&v=beta&t=fjRR9GB4hnvIYI0Uh3PH_53ZeUL26ba_loVkn-Sq9ic)

From the example above for the string <code>This is interesting book that contains interesting examples</code> we want to replace the word <code>interesting</code> with <code>JavaScript</code> in all substrings. The first method that we usually think of when replacing strings is the <code>replace()</code> method. However, as seen from the example above this method doesn't return the desired result since this method replace only the first occurrence on the substring <code>This is JavaScript book that contains interesting examples</code> but what we want is to have the string as <code>This is JavaScript book that contains JavaScript examples</code>. To achieve the desired result we usually use regular expressions for which they are unsafe to use as they contain escape characters. 

![Night](https://media-exp1.licdn.com/dms/image/C4E12AQE-0vaRDdTssw/article-inline_image-shrink_1000_1488/0/1629919429040?e=1635379200&v=beta&t=pApSisMbVwe1Ub8JoCmOkOwnay6M2EewhFv7XhhZuPc)

Without using regular expression and with the help of ES12 feature <code>replaceAll()</code> we can achieve the same result as below


![Night](https://media-exp1.licdn.com/dms/image/C4E12AQGPyPvFObLw-w/article-inline_image-shrink_1000_1488/0/1629919608177?e=1635379200&v=beta&t=RqqPVbzbW_VGCjtGpSOO5JiusMMOn1aPADfZfhyNhGQ)

---

### Logical assignment operator

As we already may know JavaScript currently support arithmetic i.e a +=b (equiv. to a = a + b) and bitwise i.e a &=b (equiv. a = a & b) assignment operators but what was missing is the ability to combine logical operator (&& || and ??) with assignment. With ES12 feature there are three such kind of logical assignment operator: 

1. Logical nullish assignment <code>(??=) -> (x ??= y)</code>  (only assigns if x is null or undefined)
2. Logical AND assignment <code>(&&=) -> (x &&=y)</code> (only assigns if x is truthy)
3. Logical OR assignment <code>(||=) -> (x ||=y )</code> (only assigns if x is falsy)

The logical nullish assignment operator performs the assignment only if the left-hand operand is nullish (<code>undefined</code> or <code>null</code>). From the example below the string <code>Superman</code> is assigned to variable user only if the user is <code>null</code> or <code>undefined</code> and not otherwise. Nullish assignment operator is much simple as we just need to write one line of code compared to the old way as we need to have an if statement and check if the variable user is <code>undefined</code> or <code>null</code> then assign the value to the user. 

![Night](https://media-exp1.licdn.com/dms/image/C4E12AQFqqxK6jwjAkg/article-inline_image-shrink_1000_1488/0/1630003600703?e=1635379200&v=beta&t=-kuyjl4CCpByfTEwPn_QFS00dhyRrONJ5NADDdU8Skc)
 
_Logical AND assignment operator_ performs assignment only if the left-hand operand is truthy. From the example below <code>num2</code> is assigned to <code>num1</code> if <code>num1</code> is truthy and not  <code>null,undefined, false, 0, or NaN</code>. 

![Night](https://res.cloudinary.com/dgo7trcso/image/upload/v1630700886/Greetings/carbon_19.png)

_Logical OR assignment operator_ is opposite of logical AND assignment operator and performs assignment if the left-hand operand is falsy. From the example below <code>num2</code> is assigned to <code>num1</code> if <code>num1</code> is <code>null, undefined, false, or 0</code>.

![Night](https://media-exp1.licdn.com/dms/image/C5612AQHy6x5FRKV8ng/article-inline_image-shrink_1000_1488/0/1630008877936?e=1635379200&v=beta&t=D4rnwLpXONeP0b00hMPbeEfwzrGb_JXTNg0I96nX6SI) 
---

### Promise.any

<code>Promise.any</code> accepts an iterable of promises and resolves as soon as one of the promise is resolved. If none of the promises aren't resolved then an <code>AggregateError</code> error is returned as an array with individual errors for each promise. <code>Promise.any</code> works in the opposite way of <code>Promise.all()</code> where all iterable promises needs to be resolved for a single promise to resolve.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1jak29ted7h6ih38x4d.png)


From the example above we have set <code>A</code> and <code>B</code> and <code>C</code> promises where A and B resolve and C rejects and as result <code>A</code> is returned since it is resolved faster than <code>B</code> and C that is rejected whereas in case  <code>D</code> and <code>E</code> we set all promises to be rejected and as a result an <code>AggregateError</code> is printed. 

---

### Private class methods
 
Since the introduction of ES6 developers can create classes in JavaScript that changed the way when thinking Object-oriented models. These classes by default have public properties and methods that are accessible from outside of the class. To overcome this issue there was a need for a naming convention with an underscore <code>(_)</code> for those properties and methods to be private even though this was just a hint and nothing preventing them from accessing outside of the class. Now with the new class features of ES12 we can define properties or methods as private by setting a <code>#</code>

![Night](https://media-exp1.licdn.com/dms/image/C4E12AQFfBvhC18T1DA/article-inline_image-shrink_1000_1488/0/1630181575350?e=1635379200&v=beta&t=KOosiBkqSEubhEcbC3c_Y7ZIuZ6cVVPRymc7r88HZ4g) 

From the example above when we try to access the method <code>#private()</code> we get an error because this method is private in the class. If we want to access this method outside of the class then we need to create a public method inside a class and call the <code>#private()</code> as seen in method <code>showAll()</code>. 

---

### Private getters and setters

In a similar way as private class method works also private getters and setters in the class.

![Night](https://res.cloudinary.com/dgo7trcso/image/upload/v1630607799/Greetings/Screenshot_2021-09-02_at_20.33.20.png)
  
---

### WeakRef 

WeakRef that stands for Weak references are primarily used to implement caches or mappings for holding large objects. JavaScript has a garbage collector mechanism that collects and automatically removes variables that are not reachable but this mechanism differs with reference objects which keeps the object in memory and leaves you with less memory. Thus, with use of WeakRef large objects are not kept alive because they appears in a cache or mapping. However you should be careful when using them and possible avoid them as advised from TC39 even though they can be useful in some cases.

![Night](https://res.cloudinary.com/dgo7trcso/image/upload/v1630608108/Greetings/Screenshot_2021-09-02_at_20.41.19.png)

From the example above we created an imaginary large object and then created Weak reference by using new instance with new WeakRef. We can access the reference by calling the method <code>deref()</code> 

### Final thoughts 

Whenever I learn and try something new I am always passionate to share my knowledge by helping other people on reaching their goals.I really hope you find this article interesting and helpful on learning ES12 features from it.

Any feedback, suggestions or recommendations are appreciated.  This will really help and motivate me for further post sharing

#### Happy coding!

![Night](https://media.giphy.com/media/Wsju5zAb5kcOfxJV9i/giphy.gif?cid=790b76118dd10c7ac98a3cb3b304f6bfeb74d046313e1ea2&rid=giphy.gif&ct=g)