The ES2018 Guide: Features and Upgrades Explained
ECMAScript, often abbreviated as ES, serves as the standard on which JavaScript is based. The latest version of this standard is ES2018, also known as ES9. In this guide, we will explore the new features and upgrades introduced in ES2018.
Rest/Spread Properties
ES6 introduced the concept of the rest element for array destructuring and spread elements for spreading array elements. ES2018 extends this concept to objects.
Rest properties allow you to extract and assign the remaining properties of an object to a new object:
1 | const { first, second, ...others } = { first: 1, second: 2, third: 3, fourth: 4, fifth: 5 }; |
Spread properties, on the other hand, enable you to create a new object by combining the properties of an existing object using the spread operator:
1 | const items = { first, second, ...others }; |
Asynchronous Iteration
ES2018 introduces the for-await-of
construct, which allows you to iterate over async iterable objects within a loop:
1 | for await (const line of readLines(filePath)) { |
Please note that for-await-of
can only be used inside async
functions, similar to the usage of await
.
Promise.prototype.finally()
The finally()
method is introduced in ES2018 to ensure a piece of code runs regardless of whether a promise is fulfilled or rejected. It is executed after the then()
and catch()
methods:
1 | fetch('file.json') |
Regular Expression Improvements
ES2018 introduces several enhancements to regular expressions.
RegExp Lookbehind Assertions
Lookbehind assertions allow you to match a string based on what precedes it. Positive lookbehinds use ?<=
, while negative lookbehinds use ?<!
. For example:
1 | /(?<=Roger) Waters/.test('Roger is my dog and Roger Waters is a famous musician'); // true |
Unicode Property Escapes
ES2018 introduces \p{...}
and \P{...}
to match Unicode character properties within regular expressions. These property escapes enable matching based on Unicode properties such as script, ASCII, digit, whitespace, emoji, and more. For example:
1 | /^\p{Lowercase}$/u.test('h'); // true |
You can also match specific Unicode values by specifying the desired property. For instance:
1 | /^\p{Emoji}+$/u.test('đđ'); // true |
For a complete list of available properties, refer to the official proposal.
Named Capturing Groups
ES2018 allows assigning names to capturing groups in regular expressions, providing a more structured way of accessing matched groups:
1 | const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/; |
The s
Flag for Regular Expressions
The s
flag, short for âsingle line,â enables the dot (.
) metacharacter in regular expressions to match newline characters as well. Without this flag, dot matches any character except newline:
1 | /hi.welcome/.test('hi\nwelcome'); // false |
tags: [âECMAScriptâ, âESâ, âES2018â, ârest/spread propertiesâ, âasynchronous iterationâ, âPromise.prototype.finally()â, âregular expression improvementsâ, âlookbehind assertionsâ, âUnicode property escapesâ, ânamed capturing groupsâ, âs flag for regular expressionsâ]