Web Island Agency

What's new in ECMAScript 2019

What's new in ECMAScript 2019
What's new in ECMAScript 2019

ECMAScript is a scripting-language specification designed to standardize JavaScript. It was first published in 1997 and ECMAScript 2019 is the tenth edition of the standard. This article is a brief summary of new features introduced in ECMAScript 2019.

Function.prototype.toString() method

The method isn’t new, but it was revised. Now the toString() method returns source code of the applied function as it is – with all white spaces, new lines, and comments. Earlier they were removed.

Use example:

function greeting(name) {
  return "Hello, " + name;
}

greeting.toString();

/*
function greeting(name) {
  return ”Hello, ” + name;
}
*/

Array.prototype.flat() and Array.prototype.flatMap()

Array.prototype.flat() creates a new flattened array by turning the specified array of values which may be more arrays to the defined depth. By default, the depth value is 1. You can use Infinity value to create a one-dimension array.

var arr1 = [a, b, [c, d]];

arr1.flat();
// [a, b, c, d]

var arr2 = [a, b, [c, d, [e, f]]];

arr2.flat();
// [a, b, c, d, [e, f]]

var arr3 = [a, b, [c, d, [e, f]]];

arr3.flat(2);
// [a, b, c, d, e, f]

Array.prototype.flatMap() is a kind of mix between array.flat() and array.map(). The method maps each element via the specified function first and then creates a new array and fills it by flattening the result.

var arr1 = ["it's rainy in", "", "London"];

arr1.map(x => x.split(" "));
// [["it's","rainy","in"],[""],["London"]]

arr1.flatMap(x => x.split(" "));
// ["it's","rainy","in", "", "London"]

String.prototype.trimStart() and String.prototype.trimEnd()

String.prototype.trimStart() removes the whitespace characters from the beggining of a string and string.prototype.trimEnd() removes them from the end. There are identical methods string.trimLeft() and string.trimRight() that are aliases of the string.trimStart() and string.trimEnd(), respectively.

var str = '   Hello!   ';

console.log(str);
// "   Hello!   ";

console.log(str.trimStart());
// "Hello!   ";

console.log(str.trimEnd());
// "   Hello!";

Object.prototype.fromEntries()

The method converts a list of key-value pairs into an object. It only accepts iterable objects (Array or Map).

var arr = [["name", "Ted"], ["surname", "Bundy"], ["age", 30]];

var obj = Object.fromEntries(arr);
// {name: "Ted", surname: "Bundy", age: 30}”

Stable Array.prototype.sort()

Earlier an array that has more than 10 items used unstable QuickSort algorithm. Now stable TimSort is used instead.

Optional catch parameter in try…catch

The method allows not to specify the unused binding (parameter) of the catch clause.

// Before:
try {
  ···
} catch (error) {
  ···
}

// Now:
try {
  ···
} catch {
  ···
}

Symbol.prototype.description

Prior to ECMAScript 2019, to get a description we used toString() method. Now we have an intuitive read-only description property - a string returning the optional description of Symbol objects.

Symbol('webisland').toString();
// "Symbol(webisland)"

Symbol('webisland').description;
// "webisland"

Symbol('').description;
// "

Improved JSON.stringify() formatting

JSON.stringify() is prevented from returning lone surrogates without representation them in UTF-8 standard. Since lone UTF-16 surrogates cannot be encoded as UTF-8, ECMAScript 2019 represents them with JSON escape sequences.

// Before:
JSON.stringify("\u{D800}"); // "�"

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

JSON superset

Now the JSON subset is a part of ECMAScript syntax. It’s a more specific update and doesn’t provide new features but fix old bugs.

Join the discussion!

The article is published under the tags