A Complete Guide to The ES2018
Posted By : Sushmit Gaur | 24-Nov-2018
Introduction
ECMAScript is the standard upon which JavaScript is based, and it's often abbreviated to as "es". Find Out everything about ECMAScript, and the latest features introduced in ES2018, aka es9.
What are the new things introduced in it?
- REST/SPREAD PROPERTIES
ES6 introduced the concept of a rest element when working with an array destructuring:const alphabets = [
"a",,
"c",
"d",
"e"]; [first, second, third, ...restOfThem] =
alphabets;
and spread elements: const
alphabets=
["a",,
"c",
"d",
"e"];
alphabetsconst sum = function(a, b, c, d, e) { return a + b + c + d + e } ; const sum = sum(...
Output => "abcde"); //
ES2018 introduced the same but for objects:
Rest operator with object:const { alpha1, alpha2, ...others } = { first: "a", second: "b", third: "c", fourth: "d", fifth: "e" }
Output =>//
1
alpha2
Output =>//
Output =>2 others //
{ third: 3, fourth: 4, fifth: 5 }
Spread can be used to create a new object by combining the properties of the object passed after the spread operator:const items = { first, second, ...others } items //
{ first: 1, second: 2, third: 3, fourth: 4, fifth: 5 }
- ASYNCHRONOUS ITERATION
An async iterable object can be used as the loop iteration with the help of new construct for-await-of :for await (const line of readLines(filePath)) { console.log(line); }
- PROMISE.PROTOTYPE.FINALLY()
When a promise is successfully fulfilled it calls then() method one after another. If something fails during this, the then() methods are skipped and the catch() method is executed. The method finally() allow to run some code regardless of the successful or unsuccessful execution of the promise:fetch('file.json') .then(data => data.json()) .catch(error => console.error(error)) .finally(() => console.log('finished'))
- REGULAR EXPRESSION IMPROVEMENTS
- REGEXP LOOKBEHIND ASSERTIONS: MATCHES A STRING DEPENDING ON WHAT PRECEDES IT?
This is a lookahead: you use ?= to match a string that is followed by a specific substring:/Juni(?=Cortez)/ /Juni(?= Cortez)/.test('Juni is my friend') //false /Juni(?= Cortez)/.test('Juni is my friend and Juni Cortez is a famous movie character') //true
?! performs the inverse operation of lookahead, matching if a string is not followed by a specific substring:/Juni(?=Cortez)/ /Juni(?= Cortez)/.test('Juni is my friend') //true /Juni(?= Cortez)/.test('Juni is my friend and Juni Cortez is a famous movie character') //false
Lookaheads uses the ?= symbol. They were already available to us.
Lookbehinds, a new feature, uses ?<=./(?<=Juni) Cortez/ /(?<=Juni) Cortez/.test('Juni is my friend') //false /(?<=Juni) Cortez/.test('Juni is my friend and Juni Cortez is a famous movie character') //true
A lookbehind is negated using ?<! :/(?<!Juni) Cortez/ /(?<!Juni) Cortez/.test('Juni is my friend') //true /(?<!Juni) Cortez/.test('Juni is my friend and Juni Cortez is a famous movie character') //false
- NAMED CAPTURING GROUPS
In ES2018 a capturing group can be assigned to a name, rather than just being assigned a slot in the result array:const regex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/ const result = regex.exec('2015-01-02') //
Output =>result.groups.year === '2015'; //
Output =>result.groups.month === '01'; //
Output =>result.groups.day === '02';
- THE S FLAG FOR REGULAR EXPRESSIONS
The s flag, short for single line, causes the . to match new line characters as well. Without it, the dot matches regular characters but not the new line:/hi.welcome/.test('hi\nwelcome') // false /hi.welcome/s.test('hi\nwelcome') // true
- REGEXP LOOKBEHIND ASSERTIONS: MATCHES A STRING DEPENDING ON WHAT PRECEDES IT?
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Sushmit Gaur
Sushmit is a self taught programmer, having experience in UI Development for Web Applications. With Experience of 3 months as an intern in React.js technology looking forward to learn new skill set and challenges for further assessment in career.