1. Logical Assignment Operators
This feature combines mathematical assignment operators with the most often used Logical operators like so `(??=, &&=, ||=)`. It provides a neatly short and concise expressive style. This concept confused me a bit at first, refactoring it from an IF statement to a Logical Assignment helped me understand how it works.
Logical OR Assignment ( || = )
- Logical OR operation does a short circuit evaluation.
- If the first operand is truthy, it returns the value. Else it returns the second operand.
- In the first example person.name is truthy so it was returned, in the second person.sex is empty therefore falsey so it returned not specified.
Logical AND Assignment ( && = )
If the first operand is truthy, changeJob() is called. Else if it’s falsey, it stops the execution and returns person.job value.
Logical Nullish Assignment ( ?? = )
- nullish operator will only assign a value to a variable if it is null or undefined.
- In this case person.location == null and was assigned lagos value.
2. Numeric Separators
3. Promise.any() + Aggregator
Promise.any() accept an array of Promise objects and returns the first promise object to be fulfilled or resolved.
- AggregateError is an object that holds rejection reasons for all promises that were rejected. In the above example error is an **AggregateError**
- Promise.any() will throw an AggregateError if all the promises were rejected.
The replaceAll() method gives developers a straightforward way of replaces a substring in a string that occurs once or more.
Unlike the String.replace() method that only changes the first substring it finds in the string with the value you are looking to replace
To get started using ES2021 features in your code, set up your project with Babel compiler, the packages have already been included in @babel/preset-env.