Tutorial

What's new in ECMAScript 2019 (ES2019) / ES10

Published on June 13, 2019
Default avatar

By Harvey Jones

What's new in ECMAScript 2019 (ES2019) / ES10

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

ECMAScript (ES for short) is a scripting language specification standardized by ECMA International in ECMA-262 and ISO/IEC 16262. It was created to standardize the JavaScript language, so as to foster multiple independent standard implementations from browser vendors. It evolves every year with new features.

The 2019 edition of the ECMAScript specification saw the addition of many new features, and here I’ll cover some of these new features. I personally love how javaScript keeps evolving and improving on a regular basis.

Array.flat()

Array.flat() returns a new array with any sub-array(s) flattened. A call to Array.flat() without any arguments will only flatten one-level deep. An optional depth argument can be provided or it can just be called consecutively.

Examples:

let arr = [1, 2, 3, [4, 5, 6, [7, 8, 9, [10, 11, 12]]]];

arr.flat(); // [1, 2, 3, 4, 5, 6, Array(4)];

arr.flat().flat(); // [1, 2, 3, 4, 5, 6, 7, 8, 9, Array(3)];

arr.flat(3); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

// Or, if you're not sure about the depth of the array:
arr.flat(Infinity); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Array.flatMap()

The flatMap() method is identical to the ES6 map method, but also flattens at the same time. The flatMap() method first maps each element using a mapping function, then flattens the result into a new array. flatMap() is often quite useful, as merging both into one method is slightly more efficient.

Examples:

let arr = [1, 2, 3, 4, 5];

arr.map(x => [x, x * 2]);
// [Array(2), Array(2), Array(2)]
// 0: (2)[1, 2]
// 1: (2)[2, 4]
// 2: (2)[3, 6]

arr.flatMap(v => [v, v * 2]);
// [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]

String.trimStart() & String.trimEnd()

String.trimStart() can be used to trim white space from the start of a string.

Examples:

let  greeting =  "    Hello everyone";

console.log(greeting.trimStart());
// "Hello everyone"
let greeting = "Hello world    ";

console.log(greeting.trimEnd());
// "Hello world"

Optional Catch Binding

Optional catch binding allows developers to use try/catch without the error parameter inside the catch block.

Examples:

Before ES2019 we use:

try {
  // some code
}
catch (err) {
  // error handling code
}

Now we can use try/catch like this with ES2019:

try  {
  // some code
}
catch {
  // error handling code
}

Object.fromEntries()

It creates an object or transforms key-value pairs into an object. It only accepts iterables e.g: Object.fromEntries(someIterable).

Examples:

let entries = new Map([["name", "john"], ["age", 22]]);

console.log(Object.fromEntries(entries));
// { name: 'john', age: 22 }

Symbol.description

The read-only description property is a string returning the optional description of Symbol objects.

Examples:

let mySymbol = `My Symbol`;

let symObj = Symbol(mySymbol);

console.log(symObj) // Symbol(mySymbol);

console.log(String(symObj) === `Symbol(${mySymbol})`); // true

console.log(symObj.description); // "My Symbol"

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Harvey Jones

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel