ES2020 Finalized
Written by Ian Elliot   
Wednesday, 08 April 2020

The eleventh version of ECMA Script has just been finalized and we can look forward to some fun things as the year progresses. So what's new?

 JSlogo

The first thing to say is that, thankfully, the changes amount to small additions. As I've said before, JavaScript is now a mature language and doesn't want for much - big changes aren't in its future. Which of the additions you consider most important is, of course, a subjective thing.

For me, given my particular interest in async coding, the addition of an allSettled method to the Promise is top of my list. We have had race and all for some time now, but these wait for the first Promise to resolve or all of them to resolve. The new method allSettled waits for all of the Promises to complete, even if they rejected. This isn't a difficult method to implement from scratch, but having it will simplify code.

Given I'm also keen on fluent method calls, or chaining, my second pick has to be optional chaining and the nullish coalescing  operator- even if they do introduce a bit of black magic symbolism.

The big problem with chaining is what happens when one of the method calls goes wrong. In particular, what happens when a object isn't defined. For example:

let result=myObject.myMethod1.?();

will check to see if myObject.myMethod1 exists and returns undefined if it doesn't. Without the optional chaining operator the result would be a runtime error. You can use optional chaining multiple times in a fluent call and you can use it with properties, functions, array access and expressions. The operator .? may look weird, but the if statements it replaces are far worse.

The nullish coalescing operator ?? is also a little weird, but it is very useful. It is a logical operator that returns its right-hand operand as long as the left-hand isn't nullish i.e. null or undefined. Its like the OR || operator but it doesn't respond to a falsy value on the left. Typically you can use ?? to assign a default value more safely than with the OR:

let myVariable=myValue ?? 42;

This sets myVariable to 42 if myValue is null or undefined, whereas:

let myVariable=myValue || 42;

sets it to 42 if myValue is falsy i.e. 0, "" etc.

This is a fix worth having.

Another function-oriented change is the introduction of the String method matchAll which produces an iterator for all matches to a regular expression. For example, you can write:

for ( match of str.matchAll(regexp)){

 and the loop will repeat for all substrings that regexp matches. You can use the method call anywhere an iterator is expected and this gives it more flexibility than you might expect.  For example:

Array.from(str.matchAll(regexp));

creates an array from the matches.

Other useful extras are BigInt for unlimited integer support, dynamic import and a new globalThis keyword that always gives you the global object, no matter what the context.

Of course, most of these features are already supported in the current browsers, but now they are official.

JSlogo

More Information

https://tc39.es/ecma262/2020/

Related Articles

ECMAScript 2018 Is Feature Complete

ECMAScript 2016 Approved

JavaScript The Language With Two Names    

The JavaScript Encyclopedia Work In Progress 

JavaScript Added To Oxford English Dictionary 

JavaScript 6 EcmaScript 2015 Final Approval 

JavaScript 20 Years Old Today 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, Facebook or Linkedin.

 

Banner


Beating Vulnerabilities in Open Source Code
31/08/2020

Open source downloads are on course to reach 1.5 trillion in 2020, an all-time high. At the same time the incidence of cyber attacks actively targeting open source software projects has increased [ ... ]



Apache Druid Adds Ranger Integration
24/08/2020

Apache Druid has been updated with better performance, easier and more flexible data ingestion, and Apache Ranger authorization integration.


More News

graphics

 



 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Friday, 10 April 2020 )