Important ES9 Javascript Feature

es2018

In this blog, we will discuss the coolest new ES9 Javascript features. I will cover the one that you guys probably should know and things that you should use in your everyday javascript world. Almost all modern browsers support ES9. You can even view our ES10 and ES11 for the latest update on EcmaScript.

ES9 Javascript Feature

Object Rest/Spread Properties for ECMAScript

If you want to extract an object by excluding some key then it can be achieved used ES9 Javascript Feature. For Example, if you have an object with the property name, age, city, house number and if you just want to extract an object that contains name and age then you can achieve using Rest/Spread Operator instead of deleting the key from the object.

Rest Properties

Rest Operator collects remaining keys that are not specified in Object and it will copy the remaining keys into a new object.

let obj = {
            name:"InfoHubBlog",
            age: 1,
            state:"West Bengal",
            country: "India"
    }
let {state,country,...withoutNameAge} = obj;
console.log(withoutNameAge) //OUTPUT: {name: "InfoHubBlog", age: 1}
ES9 Javascript
ES9 Javascript

Spread Properties

Spread Operator will copy all the keys from the provided object into a new object with the remaining keys.

let name="InfoHubBlog";
let age =1;
let stateCountryObj={state: "West Bengal",country:"India"}
let newobj = {name,age,stateCountryObj}
console.log(newobj) //OUTPUT: {name: "InfoHubBlog", age: 1, state: "West Bengal", country: "India"}
ES9 Javascript
ES9 Javascript

RegExp named capture groups

A capture group can be given a name using the (?<name>...) syntax similar to “named Groups” in Java and Python. Named groups can be accessed from properties of a groups property of the regular expression result. Numbered references to the groups are also created, just as for non-named groups.

let re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u;
let result = re.exec('2021-09-03');
// result.groups.year === '2021';
// result.groups.month === '09';
// result.groups.day === '03';

// result[0] === '2021-09-03';
// result[1] === '2021';
// result[2] === '09';
// result[3] === '03';

Asynchronous iterators

This ES9 Javascript feature supports syntactic support for asynchronous iteration with both AsyncIterable and AsyncIterator protocols. In order to provide a generic data access protocol for asynchronous data sources, ES9 introduces the AsyncIterator interface, an asynchronous iteration statement (forawaitof), and async generator functions.

for-of iteration statement which iterates over async iterable objects. An example usage would be:

for await (const line of readLinesOf40GBFile(filePath)) {
  console.log(line);
}

In the above example, 40 GB of the file will not fit in memory but if you want to integrate and read the lines synchronously then you can use for-await-of syntax.

If you have a certain thing and you are not able to process it at a single time then you can consider using for-await-of, for example, web scrapping.

Promise.prototype.finally library

Many promise libraries have a “finally” method. It works by registering a callback to be invoked when a promise is settled (either fulfilled or rejected). This use case is for precisely when you do not care about the rejection reason or the fulfilment value, and so there’s no need to provide it as an argument in a Finally callback.

Why not .then(f, f)?

  • Unlike Promise.resolve(6).then(() => {}, () => {}) (which will be resolved with undefined), Promise.resolve(6).finally(() => {}) will be resolved with 6.
  • Similarly, unlike Promise.reject(4).then(() => {}, () => {}) (which will be resolved with undefined), Promise.reject(4).finally(() => {}) will be rejected with 4..
ES9 Javascript
ES9 Javascript

RegExp Lookbehind Assertions

If you want to capture a particular value from a pattern then this can be done using RegExp Lookbehind Assertions. For Example

let pattern = “InfoHubBlog Earning is $2”

If in Pattern you just want to capture amount 2 then this can be done using RegExp Lookbehind Assertions.

Lookbehind (?<=) means Look After Particular Sentence.

let pattern = "InfoHubBlog Earning is $2";
let regEx = /(?<=InfoHubBlog Earning is).*/gm
console.log(pattern.match(regEx)) //OUTPUT: [" $2"]

Within a Lookbehind, the semantics are that match proceeds backwards–the RegExp fragment within the backreference is reversed, and the string is traversed from end to start. This has a few implications on the semantics.

Hope you like our ES9 Javascript Features blog. Please subscribe to our Blog for upcoming blogs.

Happy Coding!

Read more: Important Javascript ES10 Features, Top ES11 Features | JavascriptDifferent types of While For Loop Javascript ES6Promises in JavaScript | 10 Minute | 2021Array Destructuring | Spread Operator | Rest Operator | JavascriptECMAScript

Leave a comment

Your email address will not be published.


*