The JavaScript development community has become quite comfortable with the new language constructs introduced with ECMAScript 2015 (aka ES6). Classes, arrow syntax, template strings, let & const, map, modules and plenty more have become commonplace and browser support is very good, apart from IE11. But work on ECMAScript didn’t stop there. Here are some highlights from ES7, 8, 9 and 10, which you might not be as aware of.
ECMAScript 2016 (ES7)
Array.prototype.includes
So we can now say if(numbers.includes(2)) rather than if(numbers.indexOf(2) !== -1).Exponentiation operator
We can now do base**exponent instead of Math.pow(base, exponent).ECMAScript 2017 (ES8)
Async/Await
Yes, this one is a biggie and essentially brings the same functionality to JavaScript as was available in C#. Bear in mind that client-side JavaScript is inherently single-threaded (at the moment) so promises will still be executed one by one.Object.values
We’ve had Object.keys() for a while, to return an array of the Object’s property names. Object.values(foo) returns an array of the foo’s property values.Object.entries
In a similar vein, Object.entries(foo) returns both the property keys and values, for example, [[“a”,1],[“b”,2],…]. This might make it easier to use the object in a loop or map.String padding
We can now use padStart and padEnd to pad a string to a certain length. For example, ‘foo’.padEnd(6, ‘1234’) gives us ‘foo123’. If we replaced the 6 with a 9 we’d get ‘foo123412’.Trailing commas
You can now have a trailing comma after the last parameter to your function. If that’s your thing.ECMAScript 2018 (ES9)
Regex
You can now use named group captures by adding ?
Rest
The rest operator … (3 dots) lets us extract all the properties of an object that are not already extracted. Let’s say we have a person object with properties firstName, lastName, dob, address, niNumber. We could say let { firstName, lastName, …otherDetails } and the otherDetails property would be set to an object containing the other three properties.
Spread
Spread uses the same … operator but uses it to take an object’s properties and create them as properties in a new object. Carrying on from the example above, we could say let personDetails = { …otherDetails } and our personDetails object would be created with properties dob, address and niNumber.
Finally for promises
A finally() instance method is now available for promises, which will run as a callback after either resolve or reject has completed.