ECMAScript 2022 is right around the corner, but there were many exciting features that were introduced last year:
replaceAll method
const message = "A-message-being-divided";
const processedMessage = message.replaceAll('-', ' ');
With replaceAll(), you can replace a given character or set of characters in a string with something else. It takes two arguments - the value you want to replace and the value you want to replace it with.
Promise.any()
With Promise.any(), you are able to return a promise that resolves as soon as any of the promises passed inside the promise array parameter is fulfilled. If none are, an AggregateError will be thrown.
Logical Assignment Operators
These can help make code shorter and cleaner! There is the OR & Equals (||=) , And & Equals (&&=), and nullish coalescing assignment operator (??=)
Numeric Separators
The separators allow you to add underscores between digits, which makes them more readable.
let n1 = 1_000_000_000;
console.log(n1) // This will print:1000000000
WeakRef
WeakRef stands for Weak References and allows you o create a weak reference to an object. This is a reference that does not prevent the object from being reclaimed by the garbage collector.
Conclusion
There are always new methods and developments to learn that enhance our code. What are some of your favorite JavaScript features you use the most often? Always love hearing from everyone!
Happy coding!
Top comments (10)
Thank you, @tmchuynh ! These points are understandable and useful, but do you also know why we would use Weak Ref?
WeakRef
Why is that?
replaceAll definitely simplifies a lot of operations
Wow Great Post!!
I cannot believe I didn't know about promise.any()
Thanks! :D Always learning something new everyday
Thank your share
glad it could be of help to you
Ahhh <3 always learning every day. Thanks for explaining
Nice one! :)