ES9 (ECMAScript 2018) introduced rest/spread properties for objects, asynchronous iteration, and improvements to regular expressions.
June 2018
ES9 (ECMAScript 2018) introduced rest/spread properties for objects, asynchronous iteration, and improvements to regular expressions.
Try out JavaScript code examples and experiment with features in the official TypeScript playground.
Open JavaScript PlaygroundExtract and spread object properties similar to arrays.
const obj = { a: 1, b: 2, c: 3, d: 4 };
// Rest properties
const { a, b, ...rest } = obj;
console.log(rest); // { c: 3, d: 4 }
// Spread properties
const newObj = { ...obj, e: 5 };
console.log(newObj); // { a: 1, b: 2, c: 3, d: 4, e: 5 }Execute code regardless of whether a promise is fulfilled or rejected.
fetch("/api/data")
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error))
.finally(() => {
console.log("Request completed");
});Iterate over async iterables using for-await-of loop.
async function* asyncGenerator() {
yield Promise.resolve(1);
yield Promise.resolve(2);
yield Promise.resolve(3);
}
async function example() {
for await (const num of asyncGenerator()) {
console.log(num); // 1, 2, 3
}
}