ECMAScript 2019


JavaScript Version Numbers

Previous versions of ECMAScript were identified using numbers: ES5 and ES6.

Starting in 2016, the versions are named based on the year they are released: ES2016, 2018, 2020, and so on.

New Features in ES2019

Warning

These characteristics are quite recent.

Older web browsers might require a different code known as a Polyfill.


JavaScript String trimStart()

In 2019, JavaScript got a new method called trimStart() for handling strings.

The trimStart() function is similar to trim(), but it only deletes white spaces from the beginning of a string.

JavaScript String trimEnd()

ES2019 introduced a new method in JavaScript called trimEnd().

The trimEnd() function is similar to trim(), but it specifically removes any extra spaces from the end of a string.


JavaScript Object fromEntries()

JavaScript got a new method called fromEntries() in ES2019.

The fromEntries() function makes an object using pairs of keys and values from an iterable.


Optional catch Binding

In ES2019, you can leave out the catch parameter if you don't require it.

Example

Before 2019:

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

After 2019:

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

JavaScript Array flat()

In ES2019, JavaScript got a new method called flat() for arrays.

The flat() function makes a fresh array by straightening out a nested array.


JavaScript Array flatMap()

ES2019 introduced the flatMap() method to JavaScript arrays.

The flatMap() function takes all items from an array, processes them, and then produces a single, flattened array.


Stable Array sort()

ES2019 updated the Array sort() method.

Before 2019, the rules permitted unpredictable sorting methods like QuickSort.

After 2019, web browsers need to use a consistent sorting method:

When arranging items based on their value, each item should stay in its original place relative to other items that have the same value.

In the example above, when sorting by price, the names should not appear in a different relative position like this:

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

Revised JSON.stringify()

ES2019 updated the JSON stringify() method.

Before 2019, JSON couldn't convert characters encoded with \ into strings.

Before the ES2019 version, when using JSON.stringify() on UTF-8 code points (U+D800 to U+DFFF), it resulted in incorrect Unicode characters such as ���.

Following this update, strings containing UTF-8 code points can be securely transformed using JSON.stringify() and reversed to their original form using JSON.parse().


Separator Symbols

Now, you can use symbols (\u2028 and \u2029) to separate lines and paragraphs in string expressions.

Prior to 2019, these were considered as line terminators and caused error exceptions:

Note

JavaScript and JSON now follow the same rules.

Before ES2019:

The code text = JSON.parse('"\u2028"') will be parsed to an empty string.

The code '"\u2028"' will result in a syntax error.


Revised Function toString()

ES2019 made changes to the Function's toString() method.

The toString() function gives back a text that shows the code of a function.

Starting in 2019, the toString() function is required to provide the source code of the function, encompassing comments, spaces, and syntax details.

Prior to 2019, various web browsers provided different versions of the function, some without comments and spaces. Starting in 2019, the function should be returned exactly as it is written.