Skip to content

What's new in ECMAScript 2019

The Ecma TC39 committee responsible for the ECMAScript specification confirmed a list of features that have reached stage 4, meaning that they will become part of the ECMAScript 2019 specification. Three years ago I published “What's new in ECMAScript 2016”, two years ago “What's new in ECMAScript 2017” and year later “What's new in ECMAScript 2018”. It's time to add a few more goodies.

Optional catch binding by Michael Ficarra

You must have used a try...catch block before.

try {
  // exception is thrown
} catch (error) {
  console.error("My error handler");
}

What if you don’t need to bind the error parameter of the catch clause? Now you can skip this parameter binding. Thanks to Michael Ficarra.

try {
  // exception is thrown
} catch {
  console.error("My error handler");
}

JSON superset by Richard Gibson, Mark Miller and Mathias Bynens

This one is more of a specification update than a new language feature — it's fully backwards compatible though. Although the ECMAScript documentation calls JSON as a subset of JSON.parse(), in reality the JSON standard was not a subset of ECMAScript. JSON could contain an unescaped line separator (U+2028) and paragraph separator (U+2029) but ECMAScript must have been using an escape sequence to add them to a string. This may cause occasional bugs and adds unnecessary complexity to the specification. This proposal introduces some consistency between ECMAScript string literals and JSON string literals. The JSON standard is a legit subset of ECMAScript now.

Symbol.prototype.description by Michael Ficarra

To improve the debugging experience a Symbol can be created with an optional description. Historically we used to access this description via Symbol.prototype.toString() to return a description enclosed inside a Symbol() string. Using ECMAScript 2019 you can do this more intuitively — Symbol.prototype.description simply retrieves a description without any decorators around the string.

const foo = Symbol("My super symbol");

foo.toString();
// Symbol(My super symbol)

foo.description;
// My super symbol

Function.prototype.toString revision by Michael Ficarra

The implementation of toString() has been revised (again) and standardises the returned “implementation-dependent” string (the source code that defines the function implementation). This is an incremental update in an already biggish proposal and the rules are well defined in Function.prototype.toString proposal introduction.

function hi(name) {
  return `Hi ${name}`;
}

hi.toString();
// function hi(name) {
//   return `Hi ${name}`;
// }
Array.isArray.toString();
// function isArray() { [native code] }

Object.fromEntries by Darien Maillet Valentine

A very handy way to convert a list of key-value pairs into an object.

const arr = [["name", "Pawel"], ["surname", "Grzybek"], ["age", 31]];
const obj = Object.fromEntries(arr);
// {name: "Pawel", surname: "Grzybek", age: 31}

Well-formed JSON.stringify by Richard Gibson and Mathias Bynens

This backwards-compatible change prevents JSON.stringify() from returning code point strings without representation in UTF-8 standard.

// before
JSON.stringify("\u{D800}");
// '"�"'

// after
JSON.stringify("\u{D800}");
// "\ud800"

String.prototype.trimStart / String.prototype.trimEnd by Sebastian Markbåge and Mathias Bynens

The String.prototype.trim() has been part of the standard for years. This proposal introduces String.prototype.trimStart() and String.prototype.trimEnd(). They were added to web browsers years ago too — it is a good time to standardise them.

"   javascript   ".trim();
// "javascript"

"   javascript   ".trimStart();
// "javascript   "

"   javascript   ".trimEnd();
// "   javascript"

Array.prototype.flat / Array.prototype.flatMap by Brian Terlson, Michael Ficarra and Mathias Bynens

Do you remember SmooshGate? Array.prototype.flat() flattens arrays recursively up to a specified depth. The default depth is 1. Let's have a look at some examples:

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

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

The Array.prototype.flatMap() returns a flattened result of Array.prototype.map() method. Think of it like arr.map(mapper).flat(1).

[1, 2, 3].flatMap(item => [item, item * 100]);
// [1, 100, 2, 200, 3, 300]

Array.prototype.sort stability by Mathias Bynens

Previously arrays with more than 10 elements used an unstable QuickSort algorithm. Moving forward, this functionality is going to be replaced with stable TimSort algorithm. If you are very curious I highly recommend catching up “Getting things sorted in V8” posted by Simon Zünd from V8 team.

comments powered by Disqus