What’s new in JavaScript/Node in 2021 | ECMAScript 2021
JavaScript in 2021 provide some new features and flexibility to make JS developer life easy. let’s start —
1. Promise.any
Syntax: Promise.any([p1, p2, p3])
Promise.any() is opposite of Promise.all(), it get resolved if any of Single promise get resolved.
Example-
Promise.any get resolved as any of promise get resolved , here promise 2 resolved first.
Real world use case
If you have different API for getting weather information and want to load result very fast, so fetch multiple API asynchronously and use the first response come back. ( If you have better examples please write in comment section :))
2. Make class Method/Accessors as private
Example
Want to make showPassword and Age as Private methods. Will not directly access by class Object.
Use # with method name/accessors name to make them private.
private methods can only be accessed by Class methods. we will define public methods for this.
3. Numeric Separators
This feature enables developers to make their numeric literals more readable by creating a visual separation between groups of digits
1000000000 // Is this a billion? a hundred millions? Ten millions?
101475938.38 // what scale is this? what power of 10?
Use _ for separation
let budget = 1_000_000_000_000;
// What is the value of `budget`? It's 1 trillion!// Let's confirm:
console.log(budget === 10 ** 12); // true
4. Logical Assignment Operator
Logical assignment operator combines the logical operations(&&, || or ??) with assignment.
Example 1- && with = operator
Example 2- ||with = operator
x
is a falsy valueExample 3-
Logical assignment operator with ??
??
is Nullish Coalescing operator in JavaScript. It specifically checks if a value is null
or undefined
.
?? With Assignment Operator
Thanks for reading this post, Please give claps if you like this post. Also write your thoughts in comment section.