Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News TC39 Finalizes the Feature Set for ECMAScript 2019

TC39 Finalizes the Feature Set for ECMAScript 2019

This item in japanese

The ECMA TC39 panel recently finalized the feature set for the ES2019 edition of Javascript (also referred to as ECMAScript 2019). ES2019 improves arrays, objects, strings, symbols, try/catch blocks and JSON with new or updated features.

Arrays have two new methods on their prototype: flatMap and flat. The corresponding TC39 proposal specifies that Array.prototype.flat[ depth ] "returns a new array with all sub-array elements concatenated into it recursively up to the specified depth" with depth defaulting to 1. For instance:

> ['a', ['b','c'], ['d']].flat()
["a", "b", "c", "d"]

> ['a', ['b',['c','e']], ['d']].flat()
["a", "b", ["c", "e"], "d"]

> ['a', ['b',['c','e']], ['d']].flat(2)
["a", "b", "c", "e", "d"]

> ['a', [], ['d']].flat()
["a", "d"]

The T39 proposal specifies that Array.prototype.flatMap "first maps each element using a mapping function, then flattens the result into a new array. It is identical to a map followed by a flatten of depth 1.". For instance, the following code only inserts 'a' if cond is true:

const cond = false;
const arr = flatten([
  (cond ? ['a'] : []),

ES2019 also specifies that Array.prototype.sort must be a stable sort, that is, elements that compare equal must remain in their original order):

> var items = [
  { name: 'John', value: 37 },
  { name: 'Edward', value: 21 },
  { name: 'Sharpe', value: 37 },
  { name: 'And', value: 45 },
  { name: 'The', value: -12 },
  { name: 'Magnetic', value: 13 },
  { name: 'Zeros', value: 37 }
> items.sort(function (a, b) {
  return a.value - b.value;
0: {name: "The", value: -12}
1: {name: "Magnetic", value: 13}
2: {name: "Edward", value: 21}
3: {name: "John", value: 37}
4: {name: "Sharpe", value: 37}
5: {name: "Zeros", value: 37}
6: {name: "And", value: 45}

ES2019 adds a new static method fromEntries to Object. As the proposal states, "Object.fromEntries is proposed to perform the reverse of Object.entries: it accepts an iterable of key-value pairs and returns a new object whose own keys and corresponding values are given by those pairs":

obj = Object.fromEntries([['a', 0], ['b', 1]]); 
// { a: 0, b: 1 }

Object.fromEntries simplifies object creation in some contexts. For instance:

arr = [ { name: 'Alice', age: 40 }, { name: 'Bob', age: 36 } ];
obj = Object.fromEntries({ name, age }) => [ name, age ]));
// {Alice: 40, Bob: 36}

map = new Map([ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]);
obj = Object.fromEntries(map);
// {a: 1, b: 2, c: 3}

Strings get two new methods String.prototype.trimStart and String.prototype.trimEnd which respectively eliminates all whitespace from the beginning or end of a string:

> '  abc  '.trimStart()
"abc  "
> '  abc  '.trimEnd()
"  abc"

The Symbol Description Accessor proposal populates the Symbol prototype with a method which retrieves a symbol's description:

> const s = Symbol('The description');
> s.description
"The description"

ES2019 will allow omission of the catch binding in cases where the binding would not be used:

try {
  // try to use a web feature which may not be implemented
} catch (unused) {
  // fall back to a less desirable web feature with broader support

can now be written as:

try {
  // try to use a web feature which may not be 
} catch {
  // fall back to a less desirable web feature with broader support

ES2019 finally brings about minor changes that are mostly internal:

TC39 is the committee that evolves JavaScript. Its members are companies (among others, all major browser vendors). Each proposal for an ECMAScript feature goes through the following maturity stages:

  • Stage 0: Strawman
  • Stage 1: Proposal
  • Stage 2: Draft
  • Stage 3: Candidate
  • Stage 4: Finished

A feature will be included in the standard once its proposal has reached stage 4 and thus can be used safely. Browser support may however lag behind adoption of the feature in the standard. As of V8 v7.3 / Chrome 73, all of these ES2019 features are available by default.

Rate this Article