You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+172-1Lines changed: 172 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
34
34
| ES2018 Or ES9 | June 2018 |
35
35
| ES2019 Or ES10 | June 2019 |
36
36
| ES2020 Or ES11 | June 2020 |
37
-
37
+
| ES2021 Or ES12 | June 2021 |
38
38
39
39
### Table of Contents
40
40
@@ -100,6 +100,12 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
100
100
|7 |[globalThis](#globalthis)|
101
101
|8 |[import.meta](#import.meta)|
102
102
|9 |[for..in order](#for..in-order)|
103
+
||**ES2021 Or ES12**|
104
+
|1 |[replaceAll](#replace-all)|
105
+
|2 |[promise.any](#promise.any)|
106
+
|3 |[WeakRef](#weak-ref)|
107
+
|4 |[Numeric Separators](#numeric-separators)|
108
+
|5 |[Logical Operators](#logical-operators)|
103
109
104
110
## ES2015 Or ES6
105
111
@@ -1999,6 +2005,171 @@ Most of these features already supported by some browsers and try out with babel
1999
2005
2000
2006
**[⬆ Back to Top](#table-of-contents)**
2001
2007
2008
+
## ES2021 Or ES12
2009
+
ECMAScript 2021 or ES12 has been released in mid of 2021 with few important features which can be used JavaScript.
2010
+
2011
+
1. ### replaceAll
2012
+
The new `replaceAll()` method from `String` prototype is used to replace all the occurrences of a string from another string value. Earlier it was not possible to replace all the instances of a substring without the use of regex.
The new `promise.any` method takes multiple promises and resolves to the value of the first promise which is successfully fulfilled.
2028
+
2029
+
```javascript
2030
+
let promise1 =newPromise((resolve) =>setTimeout(resolve, 100, 'Resolves after 100ms'));
2031
+
let promise2 =newPromise((resolve) =>setTimeout(resolve, 200, 'Resolves after 200ms'));
2032
+
let promise3 =newPromise((resolve, reject) =>setTimeout(reject, 0) );
2033
+
2034
+
let promises = [promise1, promise2, promise3];
2035
+
2036
+
Promise.any(promises)
2037
+
.then( value=>console.log(value)); // Resolves after 100ms
2038
+
```
2039
+
2040
+
In case none of the promises resolved then it throws `AggregateError` exception.
2041
+
2042
+
```javascript
2043
+
(async () => {
2044
+
try {
2045
+
constoutput=awaitPromise.any([
2046
+
Promise.reject('Error 1'),
2047
+
Promise.reject('Error 2'),
2048
+
Promise.reject('Error 3'),
2049
+
]);
2050
+
console.log(`Output: ${output}`);
2051
+
} catch (err) {
2052
+
console.log(`Error: ${err.errors}`);
2053
+
}
2054
+
})();
2055
+
// Error: Error1,Error2,Error3
2056
+
```
2057
+
3. ### WeakRef
2058
+
WeakRef provides two new pieces of functionality
2059
+
1. creating weak references to objects with the WeakRef class
2060
+
2. running user-defined finalizers after objects are garbage-collected, with the FinalizationRegistry class
2061
+
2062
+
**WeakRef:**
2063
+
weak reference is a reference to an object that doesn’t prevent garbage collection if it is the only reference to the object in the memory.It’s useful when we don’t want to keep the object in memory forever(e.g, WebSocket). The main use of weak references is to implement caches or mappings to large objects for which you don't need to keep it in memory for rarely used objects.
2064
+
2065
+
Prior to ES12, WeakMaps and WeakSets are the only way to kind-of-weakly reference an object in JavaScript. Whereas WeakRef in ES12 provides actual weak references, enabling a window into the lifetime of an object.
2066
+
2067
+
Let's see an example of a weak reference object using `WeakRef` constructor and read the reference using `deref()` method
A `FinalizationRegistry` object lets you request a callback when an object is garbage-collected. It works as a cleanup callback.
2079
+
2080
+
```javascript
2081
+
// Create new FinalizationRegistry:
2082
+
constreg=newFinalizationRegistry((val) => {
2083
+
console.log(val);
2084
+
});
2085
+
2086
+
(() => {
2087
+
// Create new object:
2088
+
constobj= {}
2089
+
2090
+
// Register finalizer for the "obj" as first argument and value for callback function as second argument:
2091
+
reg.register(obj, 'obj has been garbage-collected.')
2092
+
})();
2093
+
```
2094
+
2095
+
**Note:** The finalization callback does not run immediately after garbage-collecting the event listener, so don't use it for important logic or metrics.
2096
+
4. ### Numeric Separators
2097
+
Numeric separators are helpful to read large numbers(or numeric literals) in JavaScript by providing separation between digits using underscores(_). In otherwords, numeric literals are more readable by creating a visual separation between groups of digits.
2098
+
2099
+
For example, one billion and one trillion becomes more readable with _ numeric separator,
2100
+
2101
+
```javascript
2102
+
constbillion=1000_000_000;
2103
+
console.log(billion); // 1000000000
2104
+
2105
+
consttrillion=1000_000_000_000n; // BigInt number
2106
+
console.log(trillion); // 1000000000000
2107
+
```
2108
+
2109
+
It can be used for binary and hex literals as well.
2110
+
2111
+
```javascript
2112
+
constbinaryLiteral=0b1010_1010;
2113
+
console.log(binaryLiteral);
2114
+
consthexLiteral=0xFF_FF_FF_FF;
2115
+
console.log(hexLiteral);
2116
+
```
2117
+
2118
+
**Note:** The separator can be placed anywhere within the number for readability purpose.
2002
2119
2120
+
5. ### Logical Operators
2121
+
Logical assignment operators combines the logical operations(&&, || or ??) with assignment. They are quite useful for assigning default values to variables.
2122
+
**&&=:**
2123
+
The &&= operator performs the assignment only when the left operand is truthy.
2124
+
```javascript
2125
+
let x =10;
2126
+
let y =20;
2127
+
x &&= y;
2128
+
console.log(x); // 20
2129
+
```
2003
2130
2131
+
The above logical assignment operation can be expanded to:
2004
2132
2133
+
```javascript
2134
+
x = x && (x = y);
2135
+
(OR)
2136
+
if (x) {
2137
+
x = y;
2138
+
}
2139
+
```
2140
+
**||=:**
2141
+
The ||= operator performs the assignment only when the left operand is falsy.
2142
+
```javascript
2143
+
let x =0;
2144
+
let y =20;
2145
+
x ||= y;
2146
+
console.log(x); // 20
2147
+
```
2148
+
2149
+
The above logical assignment operation can be expanded to:
2150
+
2151
+
```javascript
2152
+
x = x || (x = y);
2153
+
(OR)
2154
+
if (!x) {
2155
+
x = y;
2156
+
}
2157
+
```
2158
+
**??=:**
2159
+
The ??= operator performs the assignment only when the left operand is null or undefined.
2160
+
```javascript
2161
+
let x;
2162
+
let y =1;
2163
+
x ??= y;
2164
+
console.log(x); // 1
2165
+
```
2166
+
2167
+
The above logical assignment operation can be expanded to:
0 commit comments