There square measure 2 smart reasons for this:
- JavaScript is that the language of the net, and therefore the internet is chop-chop increasing.
- Because of this, JavaScript is receiving an incredible quantity of attention from developers World Health Organization square measure up its performance and increasing its practicality.
Here are the top new features of es2021:
1. Promise.any
Promise.any accepts a series of guarantees and is resolved as presently as any guarantees created square measure resolved. Sounds cool right? let American state show you Associate in Nursing example
Promise.any([
fetch(‘https://one.com’).then(()=>)
fetch(‘https://two.com’).then(()=>)
fetch(‘https://three.com’).then(()=>)
]).then((res)=>the guarantees was consummated
console.log() // one OR 2 OR 3
}).catch((error) = > )
})
We created 3 requests right away. once one among the requests is resolved, Promise. any additionally resolves and submits the primary application resolved to the console (for example, by Google)
If all guarantees square measure rejected, promise.any throws a replacement sort of error AggregateError.
What’s therefore new regarding the AggregateError item represents a slip once many errors square measure committed in one error.
Here’s what it’s like:
Promise.any([
fetch(‘https://one.com’).then(()=>)
fetch(‘https://two.com’).then(()=>)
fetch(‘https://three.com’).then(()=>)
]).then((res)=>the guarantees was consummated
console.log() // one OR 2 OR 3
}).catch((error) = > )
2. Logical Assignment Operator
Logical assignment operator means that it’ll be a mix of the logical operators (&&, || or ??) with the assignment.
var a = 10
var b = 20
a &&= b
console.log(a)
The result’s a pair of. on top of may be like below
a && (a = b)
OR
if(a)
It merely means if a then assign a to b , this was the instance of && during a similar fashion, we are able to use || and ??.
a &&= b
a ||= b
a ??= b
let’s inspect the instance of ||
let a = 10
let b = 20
a ||= b
console.log(a)
SO the answer is one. It may be re-written as
a || (a = b)
Let’s inspect ?? , it’s Nullish Coalescing operator and it had been in the main helpful after you ought to check if the worth isnull or indefinable.
let a
let b = a ?? five
console.log(b)
The results of this console is five At line no a pair of, if the worth of a is null or indefinable, the right-hand facet of ?? is dead and it’ll be the worth of b
Let American state offer you one complicated example?? with =
let a
let b = 2
a ??= b
console.log(x)
SO, the result’s a pair of. Here as you recognize the worth of ais indefinable. that the right-hand facet expression is dead then a can be2.
3. replaceAll()
String.prototype.replaceAll() can replace all occurrences of a string with another string worth.
As of now, JS string incorporates a replace() technique and you would possibly grasp that it may be helpful to interchange a string absolutely.
const str = “Hey Frodo, Hey Joe”
const newStr = str.replace(“Hey”, “Hello”)
console.log(newStr) // “Hello Frodo, Hey Joe”
You can see that replace() can replace solely the primary incidence within the string, not the complete, currently you would possibly wish to interchange all. What are you able to do? affirmative, one choice is you’ll be able to use RegEx or E 2021 got you,
const str = “Hey Frodo, Hey Joe”
const newStr = str.replaceAll(“Hey”, “Hello”)
console.log(newStr) // “Hello Frodo, Hello Joe”
Simple? a great deal.
4. non-public Accessor
In ES2021, to outline non-public accessor is incredibly straightforward, you simply ought to place # before the function/method name.
class automotive come “Audi” }
set name(value)
// non-public accessor
get #KMs() come 16500 }
set #age(value)
}
If you get your hands on these new options of ES2021, as of currently you’ll be able to it via the babel plugin
Logical Assignment Operator
Numeric extractor
5.WeakRef
WeakRef is that the shorthand for Weak References, and its primary use is to carry a weak relevance another object. meaning it doesn’t forestall the rubbish collector from assembling the thing. The Weak Reference is beneficial once we don’t need to stay the thing within the memory forever.
But why can we want the WeakRef within the initial place? In JavaScript, the thing isn’t collected by the rubbish collector as long as a relevance that object exists. Thus, it keeps the thing within the memory, that leaves you with less memory. The WeakRef implementation permits you to avoid that.
You can produce a Weak Reference by victimisation new WeakRef, and you’ll be able to scan a reference by career the deref() methodology. a straightforward example would be:
const largeObject = new WeakRef({
name: “CacheMechanism”,
type: “Cache”,
implementation: “WeakRef”
});
largeObject.deref();
largeObject.deref().name;
largeObject.deref().type;
largeObject.deref().implementation;
6.Numeric Separators
Working with massive numbers will quickly become confusing. as an example, take into account the quantity “92145723”. you’ve got to pay shut attention to visualize that it’s ninety two million and one thing.
With the new addition from ES2021, you’ll be able to re-write a similar variety as follows “92_145_723”. That is, you employ underscores to boost the readability. you’ll be able to see it’s already higher, and easier to know the quantity. Now, you’ll be able to clearly see it’s ninety two million and one thing.
Therefore, the numeric apparatus feature is just for rising readability. It doesn’t have an effect on the performance of your application negatively or absolutely.
// previous syntax before ES12
const variety = 92145723;
// new syntax coming back with ES12
const variety = 92_145_723;
7.BigInt
BigInt is one among the foremost anticipated options in JavaScript that is currently finally here. It really permits developers to possess abundant larger whole number illustration in their JS code for processing for knowledge handling.
8.Dynamic import
Dynamic imports in JavaScript natively offer you the choice to import JS files dynamically as modules in your application. this is often a bit like however you are doing it with Webpack and Babel at the instant.
9.Nullish Coalescing
Nullish coalescing adds the flexibility to really check nullish values rather than falsey values. what’s the distinction between nullish and falsey values, you would possibly ask?
In JavaScript, a great deal of values square measure falsey, like: empty strings, the quantity zero, undefined, null, false, NaN, etc.
10.Optional Chaining
Optional chaining syntax permits you to access deeply nested object properties without concern if the property exists or not. If it exists, great! If not, you will get indefinable came.
11.String#matchAll
matchAll may be a new technique superimposed to the String epitome that is expounded to Regular Expressions. This returns Associate in Nursing iterator that returns all matched teams one when another.