bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/JavaScript/Debugging, Projects, and Reference
JavaScript•Debugging, Projects, and Reference

ECMAScript 2018

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind ECMAScript 2018?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ await () {}
3Order

Put the learning moves in the order that makes the concept easiest to apply.

JavaScript Asynchronous Iteration
New RegExp Features in 2018
New Features in JavaScript 2018

New Features in JavaScript 2018

FeatureDescription
Asynchronous IterationAllows the await keyword in for/of loops
Promise FinallySchedules a function to be executed when a promise has been "fulfilled" or "rejected"
Array Rest ElementsAllows to destruct an array and collect the leftovers
Object Rest PropertiesAllows to destruct an object and collect the leftovers
Shared MemoryAllows different parts of a program to access the same memory

New RegExp Features in 2018

FeatureDescription
/sAllows the . (dot) metacharacter to match line terminators
\p{}Matches character with a Unicode character property
(?<=y)(?<=y)x matches "x" if "x" is preceded by "y"
(?<!y)(?<!y)x matches "x" if "x" is NOT preceded by "y"
(?<name>)Captures text and names (labels) it

Browser Support

ECMAScript 2018 is supported in all modern browsers since Jun 2020 :

Chrome 64Edge 79Firefox 78Safari 12Opera 51
Jan 2018Jan 2020Jun 2020Sep 2018Feb 2018

JavaScript Asynchronous Iteration

ECMAScript 2018 added asynchronous iterators and iterables.

Example

for await () {}

JavaScript Promise.finally()

ECMAScript 2018 finalizes the full implementation of the Promise object with Promise.finally :

Promise.finally() defines a function to be executed when a promise has either been successfully resolved rejected.

Example

let myPromise = new Promise();
myPromise.then();
myPromise.catch();
myPromise.finally();

JavaScript Array Rest Elements

ECMAScript 2018 added the rest operator (...).

Example 1

let a, rest;
const arr1 = [1,2,3,4,5,6,7,8];
[a, ...rest] = arr1;

Example 2

let a, b, rest;
const arr1 = [1,2,3,4,5,6,7,8];
[a, b, ...rest] = arr1;

JavaScript Object Rest Properties

ECMAScript 2018 added the rest operator (...).

This allows us to destruct an object and collect the leftovers onto a new object:

Example

// Create an Object: const car = {type:"Fiat", model:"500", color:"white"}; // Destructure the Object
let {type, model, color} = car;
document.getElementById("demo").innerHTML = "The car type is: " + type;

New JavaScript RegExp Features

ECMAScript 2018 added 4 new RegExp features:

  • /s (dotAll) Flag
  • Unicode Property Escapes (\p{...})
  • Lookbehind Assertions (?<=y)x and (?<!y)x
  • Named Capture Groups

RegExp /s Flag

Example

let text = "Line\nLine.";
let pattern = /Line./gs;
let result = text.match(pattern);

The s (dotAll) flag allows the . (dot) metacharacter to match any character, including line terminator characters (like \n, \r, \u2028, \u2029).

Without s , \n does not match line terminators.

RegExp \p Metacharacter

Example

let text = "Hello 😄";
let pattern = /\p{RGI_Emoji}/v;
let result = pattern.test(text);

The \p{Unicode Property} metacharacter matches any character with a Unicode character property.

RegExp Lookbehind

Example (?<=y)x

let text = "Hello ExampleSite";
let pattern = /(?<=Hello )ExampleSite/;
let result = pattern.test(text);

(?<=y)x matches "x" if "x" is preceded by "y".

Negative Lookbehind

Example (?<!y)x

let text = "Hello ExampleSite";
let pattern = /(?<=Hello )ExampleSite/;
let result = pattern.test(text);

(?<!y)x matches "x" if "x" is NOT preceded by "y".

RegExp Named Capturing Groups

Example (?<name>...)

const text = "Name: John Doe";
// Using named capturing groups
const regex = /(?<firstName>\w+) (?<lastName>\w+)/;
const match = text.match(regex);
let fName = match.groups.firstName;
let lName = match.groups.lastName;

Explained

  • (?<firstName>\w+) captures a word and labels it firstName
  • (?<lastName>\w+) does the same for lastName
  • text.match() returns an array with a groups property
  • match.groups() returns an object: {firstName:"John", lastName:"Doe" }

When using a regular expression with capturing groups, the match() method of a string returns a result array that includes a groups property. This groups property is an object that holds the matches for named capturing groups.

JavaScript Threads

In JavaScript you use the Web Workers API to create threads.

Worker threads are used to execute code in the background so that the main program can continue execution.

Worker threads run simultaneously with the main program. Simultaneous execution of different parts of a program can be time-saving.

JavaScript Shared Memory

Shared memory is a feature that allows threads (different parts of a program) to access and update the same data in the same memory.

Instead of passing data between threads, you can pass a SharedArrayBuffer object that points to the memory where data is saved.

SharedArrayBuffer

A SharedArrayBuffer object represents a fixed-length raw binary data buffer similar to the ArrayBuffer object.

Previous

ECMAScript 2019

Next

ECMAScript 2017