14

ES12 is Going to Make Your Life Easier!

 3 years ago
source link: https://javascript.plainenglish.io/es12-is-going-to-make-your-life-easier-6be8d131e117
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
ES12 is Going to Make Your Life Easier!

ES12 is Going to Make Your Life Easier!

Learn how to include the awesome upcoming features of ECMAScript 2021 in your current codebase.

đź“·

JavaScript is a language that is getting better every year. Since the appearance of ES6 in 2015, we’re witnessing a vibrant evolution in the language. Every year some new features are being added to it.

This year we are going to get some really awesome features that will make a whole lot of things easier for the developers.

Let us unravel some of them!

Scared of RegExp? No Worries, the replaceAll method is here!

Regular expressions have a somewhat daunting reputation. They are nonetheless regarded with equal measures of awe and dread even by some experienced programmers.

“Don’t ever get too comfortable, you can be replaced.”

— Anonymous

In JavaScript, there is no way to replace all instances of a substring in a string without the use of a global RegExp (/RegExp/g).

Love for mangoes — love for life

It is summertime. For many, summer means mangoes. All of my smoothies always have mangoes in them. Who doesn’t like mangoes?

1*3nQ9wvmvapXExldGxXIIxg.png?q=20
es12-is-going-to-make-your-life-easier-6be8d131e117
We could only replace one apple!

To replace all the apples, we have to use RegExp — not everyone is comfortable using it.

1*I-rIEseXYVQiza4UpZc4CA.png?q=20
es12-is-going-to-make-your-life-easier-6be8d131e117

A new replaceAll method has been added to the String prototype. The replaceAll method is a sequel to the String.replace method, which only replaces the first occurrence of the pattern with replacement.

1*onma4Px25-4berYX_AymIw.png?q=20
es12-is-going-to-make-your-life-easier-6be8d131e117

Now we can replace all the apples with mango without the use of RegExp.

1*091Z35yKVZTClpfKIgQtLg.png?q=20
es12-is-going-to-make-your-life-easier-6be8d131e117

Wow! This looks so delicious.

The replaceAll method allows you to easily replace stuff on strings, you don’t need to use regex anymore in such cases. This feature can come in handy in many scenarios especially if you are building a word processor app.

Trouble making decisions? Assignment Operators are here to help.

One of the essential things we do in programming is to make decisions — the better you are at it — the better the design of your code will be.

“Never make a permanent decision on temporary feelings.”

— Gautama, The Buddha

In Programming, we make use of logical assignment operators to make decisions. ES12 has introduced valuable useful logical assignment operators,

??= &&= ||=

??=

1*JjYFnb_KAJRQKbh6f08HOA.png?q=20
es12-is-going-to-make-your-life-easier-6be8d131e117

Great piece of advice, but the ??= operator can make it look slick.

1*DysrU3BaH2GB9iEKXoKMww.png?q=20
es12-is-going-to-make-your-life-easier-6be8d131e117
This checks if the sense is either null or undefined then assigns a value to it.

&&=

1*pDJ9MM65efWve6y2IqN35Q.png?q=20
es12-is-going-to-make-your-life-easier-6be8d131e117
1*KpFnM5fwZQPGdfHVJUSEwA.png?q=20
es12-is-going-to-make-your-life-easier-6be8d131e117
This statement checks if and only if the person has truthy value then only it will assign it a new value

||=

1*qVWvjtLpiw1SeZKAa2AV4w.png?q=20
es12-is-going-to-make-your-life-easier-6be8d131e117
1*7_8TaxSLGAfeqfEAWoTsPA.png?q=20
es12-is-going-to-make-your-life-easier-6be8d131e117

The important difference between ??= and ||= is that the initial one checks the value for both null or undefined the other one checks just for a falsy value.

Having tough times with the numbers? Underscore can improve your score!

1*MmUssp3dU2u4rhgNJ0z1gg.png?q=20
es12-is-going-to-make-your-life-easier-6be8d131e117

“Numbers are intellectual witnesses that belong only to mankind.”

— Honore de Balzac

1*HXO3dMEIySIX06b7ht2ToQ.png?q=20
es12-is-going-to-make-your-life-easier-6be8d131e117
How much is it? Let me know in the comment section.

Know the first one and be happy with it.

In programming, many times we asynchronously do things. That is we initiate something and before even it completes we initiate some other task.

ES6 introduced promises to handle the asynchronous task sensibly. Sometimes we can initiate many promises object — a placeholder for the eventual result.

ES12 gives us a function Promise.any() which can help us to know which promise object got finished first.

Promise.any() takes an array of promises as an argument. Of all the promises that are resolved, the one which gets resolved first will be returned by Promise.any() . If all the promises are rejected, you will get an error.

In real life suppose you proposed 3 different people for a date and you want to know which one replied with an affirmative answer first.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK