ES9 Ecmascript 2018

Introduction

ECMAScript 2018, also known as ES9, continued the evolution of JavaScript with the introduction of new features and enhancements that targeted both the syntax and functionality of the language. Released in June 2018, ES9 aimed to improve the developer experience by simplifying complex operations and enhancing the language’s ability to handle asynchronous operations and powerful data processing.

ECMAScript 2018 Language Specification

Detailed Feature Explanation with Examples

1. Asynchronous Iteration

Explanation:

  • Asynchronous iteration allows for-await-of loops to iterate over asynchronous data sources like streams or APIs.
  • It works with objects that implement the asynchronous iteration protocol.

Example:

javascript
	async function asyncIterable() {
	  for await (let value of someAsyncIterable) {
	    console.log(value);
	  }
	}

2. Rest/Spread Properties for Objects

Explanation:

  • Similar to arrays, objects now support rest properties for a more concise way to collect properties into a new object.
  • Spread properties allow the expansion of an object’s own enumerable properties.

Example:

javascript
	let { a, ...x } = { a: 1, b: 2, c: 3 };
	console.log(x); // { b: 2, c: 3 }
	
	let obj = { a: 1, b: 2 };
	let copy = { ...obj };

3. Promise.prototype.finally

Explanation:

  • finally() method is used with promises to execute a callback function when the promise is settled, regardless of its outcome.
  • It helps in running cleanup code or finalizing operations.

Example:

javascript
	fetch('https://api.example.com')
	  .then((data) => data.json())
	  .catch((error) => console.error('Error:', error))
	  .finally(() => console.log('Fetch attempted'));

4. RegExp Improvements

Explanation:

  • ES9 introduced several improvements to regular expressions, including named capture groups, Unicode property escapes, and lookbehind assertions.

Example:

javascript
	let regex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
	let match = regex.exec('2018-12-24');
	console.log(match.groups.year); // 2018

5. Template Literal Revision

Explanation:

  • Allows tagged templates to have strings with previously illegal escape sequences.

Example:

javascript
	function tag(strings) {
	  return strings.raw[0];
	}
	console.log(tag`string text line 1 \n string text line 2`);

Common Pitfalls

  • Misunderstanding the use of asynchronous iteration in regular for-loops.
  • Overlooking the power of RegExp improvements for complex string manipulations.

Further Reading

Summary

ES9’s features, particularly asynchronous iteration and improvements to regular expressions, represent significant steps forward in the language’s evolution. The enhancements not only simplify complex operations but also introduce new capabilities that make JavaScript more powerful and versatile for modern web development. Understanding and effectively using these features is key for any JavaScript developer looking to write efficient, clean, and advanced code in today’s web development landscape.