JavaScript - ECMAScript 2018
JavaScript - ECMAScript 2018
TheECMAScript 2018version of JavaScript was released in 2018. ECMAScript 2017 introduced singnificant enhancement to the language. Two important features introduced in this version areasynchronous iterationfor improved handling of asynchronous operations, andpromise finally()to execute code regardless of promise resolution. This chapter will discuss all the new added features in ECMAScript 2018.
New Added Features in ECMAScript 2018
Here are the new methods, features, etc., added to the ECMAScript 2018 version of JavaScript.
- Asynchronous iteration
- New features of the RegExp() Object
- Promise.finally()
- Rest object properties
Here, we have explained each feature in detail.
JavaScript Asynchronous Iteration
You can also use the βawaitβ keyword with the for loop to make asynchronous iterations.
For example, you are iterating multiple promises, and in each iteration, you need to stop the code execution until the current promise gets resolved or rejected.
Example
In the below code, we have defined the async generator function named gen_function. The gen_func() function makes 5 iterations using the loop. In each iteration, it awaits to resolve the promise and returns p.
In the test() function, we used the βawaitβ keyword with the for loop to make asynchronous iterations. It updates the output after every 0.5 seconds.
Output
Returned element is: 0
Returned element is: 1
Returned element is: 2
Returned element is: 3
Returned element is: 4New features of the RegExp() object
In ECMAScript 2018, the following four new regular expression features were introduced β
- Unicode Property Escapes (\p{β¦})
- Lookbehind Assertions
(?<=)and(?<!)`- Named Capture Groups - s (dotAll) Flag
Unicode Property Escapes (\p{β¦})
The Unicode property escape allows you to escape the Unicode character. You need to represent the Unicode in the curly braces followed by the β\pβ.
Example
In the below code, we use the regular expression to check whether the tet contains the letter using the Unicode property access.
Output
regex.test('Y'): true
regex.test('6'): falseLookbehind Assertions (?<=) and (?<!)
`
The Lookbehind assertion allows you to find a particular subpattern followed by a particular sub patter. The positive look-behind assertion is equal to ?<=, and the negative look-behind assertion is ?<!.
Example
In the below code, we are looking for a word after the β@β using the look behind the assertion. It finds words after the β@β pattern.
Output
lookBeind.exec('abcd@domain.com')[0]: codescompilerNamed Capture Groups
You can give a unique name to each group of the regular expression. The group names make it easy to extract the patterns from the string.
Example
In the code below, we have defined the regular expression to match the date pattern. Also, we have named the groupsβ year, month, and day.
After that, we extracted the year from the date using the group name.
Output
The year of the date is: 2023s (dotAll) Flag
The β.β (dot) character matches any character except the new line in a regular expression. If you also want to match the new line using the β.β Character, you need to use the β/sβ flag, as shown in the example below.
Example
In the below code, we have added the β.β Character in the regular expression pattern to match any character, and also added \s flag.
In the output, you can see that β.β character also matches with the β\nβ.
Output
strRegex.test('Hello\nprogrammers'): trueJavaScript Promise finally()
You can use the finally() block with promises to execute the particular code after the promise gets resolved or rejected. It is similar to the tryβ¦catchβ¦finally block.
Example
In the example below, we created the promise and stored it in the getData variable. The promise gets resolved after 1000 milliseconds.
After that, we use the βthenβ¦finallyβ, block to execute the promise. In the output, you can observe that code of the βfinallyβ block always gets executed.
Output
Promise resolved!
In the finally block!JavaScript Rest Object Properties
You can use the spread operator while destructuring the objects. The spread operator allows you to collect the remaining properties in a single variable in the object format.
Example
In the example below, the numbers object contains 4 properties. While destructuring, we get the value of the num1 property and store other properties in the nums variable using the spread operator.
Output
num1 = 40
nums = {"num2":50,"num3":80,"num4":90}