ECMAScript 2019

JavaScript version number

Old JS versions are named by number: ES5 (2009) and ES6 (2015).

Starting from 2016, versions are named by year: ECMAScript 2016, 2017, 2018, 2019, ...

New features in ES2019:

Warning

These features are relatively new.

Older browsers may need alternative code (Polyfill)

JavaScript String Method trimStart()

ES2019 added the String method trimStart().

trimStart() The working method of the trim() Similarly, but only remove spaces from the beginning of the string.

Example

let text1 = "     Hello World!     ";
let text2 = text1.trimStart();

Try It Yourself

Since January 2020, all modern browsers have supported JavaScript String trimStart():

Chrome Edge Firefox Safari Opera
Chrome 66 Edge 79 Firefox 61 Safari 12 Opera 50
April 2018 January 2020 June 2018 September 2018 May 2018

JavaScript String Method trimEnd()

ES2019 added to JavaScript trimEnd() string method.

trimEnd() The working method of the trim() Similarly, but only remove spaces from the end of the string.

Example

let text1 = "     Hello World!     ";
let text2 = text1.trimEnd();

Try It Yourself

Since January 2020, all modern browsers support JavaScript String trimEnd():

Chrome Edge Firefox Safari Opera
Chrome 66 Edge 79 Firefox 61 Safari 12 Opera 50
April 2018 January 2020 June 2018 September 2018 May 2018

JavaScript Object Method fromEntries()

ES2019 added to JavaScript fromEntries() Object method.

fromEntries() The method creates an object from iterable key/value pairs.

Example

const fruits = [
["apples", 300],
["pears", 900],
["bananas", 500]
];
const myObj = Object.fromEntries(fruits);

Try It Yourself

Since January 2020, all modern browsers support JavaScript String trimEnd():

Chrome Edge Firefox Safari Opera
Chrome 73 Edge 79 Firefox 63 Safari 12.1 Opera 60
March 2019 January 2020 October 2018 March 2019 April 2019

Optional catch Binding

Starting from ES2019, if not necessary, the catch parameter can be omitted:

Example

Before 2019:

try {
// code
} catch (err) {
// code
{}

After 2019:

try {
// code
} catch {
// code
{}

Since January 2020, all modern browsers support optional catch binding:

Chrome Edge Firefox Safari Opera
Chrome 66 Edge 79 Firefox 58 Safari 11.1 Opera 53
April 2018 January 2020 January 2018 March 2018 May 2018

JavaScript Array Method flat()

ES2019 added to JavaScript flat() Array method.

flat() The method creates a new array by flattening nested arrays.

Example

const myArr = [[1,2],[3,4],[5,6]];
const newArr = myArr.flat();

Try It Yourself

Since January 2020, all modern browsers support JavaScript Array flat():

Chrome Edge Firefox Safari Opera
Chrome 69 Edge 79 Firefox 62 Safari 12 Opera 56
September 2018 January 2020 September 2018 September 2018 September 2018

JavaScript Array Method flatMap()

ES2019 added to JavaScript flatMap() Array method.

flatMap() The method first maps all elements of the array, and then creates a new array by flattening the array.

Example

const myArr = [1, 2, 3, 4, 5, 6];
const newArr = myArr.flatMap((x) => x * 2);

Try It Yourself

Stable Array Method sort()

ES2019 Revisionof Array sort() method.

Before 2019, the specification allowed unstable sorting algorithms, such as QuickSort.

After ES2019, browsers must use stable sorting algorithms:

When sorting elements based on a value, these elements must maintain their relative positions with other elements having the same value.

Example

const myArr = [
  {name:"X00",price:100 },
  {name:"X01",price:100 },
  {name:"X02",price:100 },
  {name:"X03",price:100 },
  {name:"X04",price:110 },
  {name:"X05",price:110 },
  {name:"X06",price:110 },
  {name:"X07",price:110 }
];

Try It Yourself

In the example above, when sorting by price, the results are not allowed to appear in other relative positions of the names, as follows:

X01 100
X03 100
X00 100
X03 100
X05 110
X04 110
X06 110
X07 110

Revised JSON.stringify()

ES2019 Revisionof JSON's stringify() method.

Before 2019, JSON could not stringify characters encoded with \.

Example

let text = JSON.stringify("\u26D4");

Try It Yourself

Before ES2019, using JSON.stringify() on UTF-8 code points (U+D800 to U+DFFF) would return corrupted Unicode characters, such as ���.

After this revision, strings with UTF-8 code points can be safely converted using JSON.stringify() and restored to the original string using JSON.parse().

Separators

Line separators and paragraph separators are now allowed in string literals (\u2028 and \u2029)

Before 2019, these were considered line terminators and caused error exceptions:

Example

// This is valid in ES2019:
let text = "\u2028";

Try It Yourself

Note

Now, JavaScript and JSON have the same rules.

Before ES2019:

text = JSON.parse('"\u2028"') will parse to ''.

text = '"\u2028"' will result inSyntax error.

Revised Function toString()

ES2019 RevisionThe Function toString() method.

The toString() method returns a string representing the source code of the function.

Starting from 2019, toString() must return the source code of the function, including comments, spaces, and grammatical details.

Before 2019, different browsers returned different function variants (such as without comments and spaces). Starting from 2019, the function should be returned exactly as written.

Example

function myFunction(p1, p2) {
  return p1 * p2;
{}

Try It Yourself