ECMAScript 2019 (ES2019 or ES10) New Features -JavaScript
In June 2019, the TC39 committee published another set of changes to the ECMAScript standard, appropriately called ECMAScript 2019 (or ES2019). These changes are listed as
Array.prototype.{flat,flatMap}
The ES2019
release has added two new methods to Arrays
1) .flat()
2) .flatMap()
.flat()
This method is used to “flattens” an Array.
Syntax: <Array>.flat(depth);
//default depth is 1
Example
.flatMap()
This method is same as first calling .map()
and then flattening the result using flat()
method.
Syntax: <Array>.flatMap(func);
Note: It is same as <Array>.map(func).flat(1);
Example:
Object.fromEntries
ES2019
introduced new static method Object.fromEntries
which perform reverse of Object.entries
.
It accepts list of key-value
pairs and returns a new object
whose own keys
and corresponding values
are given by those pairs.
Example
String.prototype.{trimStart,trimEnd}
JavaScript already supports removing all whitespace from both ends of a string using .trim()
method but ES2019
comes with two more method ‘trimStart
’ & ‘trimEnd
’ which are used only to trim either at start or end of a string.
Example:
Symbol.prototype.description
This is new ESCAScript
update to primitive data type
Symbol.
Earlier way to access the description was by converting the symbol
to a string.
Example
const sym = Symbol(‘The description’);
assert.equal(String(sym), ‘Symbol(The description)’);
But ES2019
update introduces the getter Symbol.prototype.description
to access the description directly. E.g.
assert.equal(sym.description, 'The description');
You can explore more about this update on TC39 Official proposal here .
Optional catch binding
This update allow developers to use try/catch without creating an unused binding.
You are free to go ahead make use of catch block without a param.
Stable Array.prototype.sort()
Array.prototype.sort() method used a unstable quicksort
algorithm, when sorting
arrays with more than 10 elements. To ensure that the array is aligned correctly, ECMAScript 2019
uses the Timsort
(hybrid sorting derived from merge sort
and insertion sort
) algorithm for the Array.prototype.sort().
This specification currently works well with all JavaScript engines. However, Microsoft Edge with ChakraCore
generates a sort error with array containing 512+ elements.
The below screenshot illustrates the stability test results on Edge and Firefox. As you can see Edge is failing.
More detailed discussion here: “Array#sort stability”.
Changes that are mostly internal
Well-formed JSON.stringify
According to the RFC 8259 for JSON, if you exchange JSON “in public”, you must encode it as UTF-8
.
That can be a problem if you use JSON.stringify()
, because it may return sequences of UTF-16
code units that can’t be encoded as UTF-8
(e.g. characters in the range 0xD800
to 0xDFFFF
, which are classified as ‘surrogate
’)
The ECMAScript 2019, returns an escape sequence instead of returning an invalid Unicode string
, as shown(Edge) in the following illustration.
For more details checkout TC39
official proposal documentation.
JSON superset
JSON (as standardised via ECMA-404
) is not a subset of ECMAScript. It means ECMAScript string literals
and JSON string literals
are two different entities.
ECMAScript string literals couldn’t contain the characters U+2028 LINE SEPARATOR
and U+2029 PARAGRAPH SEPARATOR
. For example
ECMAScript string literals
const sourceCode = '"\u2028"';
eval(sourceCode); // SyntaxError
JSON string literals
const json = '"\u2028"';
JSON.parse(json); // OK
ECMAScript 2019 remove the restriction for ECMAScript string literals
because you don’t need separate rules for ECMAScript string literals
and JSON string literals
.
Function.prototype.toString revision
ECMAScript 2019 brings two major improvements compared to ES2018:
- Whenever possible — source code: If a function was created via ECMAScript source code,
toString()
must return that source code. In ES2016, whether to do so is left up to engines. - Otherwise — standardised placeholder: In ES2016, if
toString()
could not (or would not) create syntactically valid ECMAScript code, it had to return a string for whicheval()
throws aSyntaxError
. In other words,eval()
must not be able to parse the string. This requirement was forward-incompatible — whatever string you come up with, you can never be completely sure that a future version of ECMAScript doesn’t make it syntactically valid. In contrast, the proposal standardises a placeholder: a function whose body is{ [native code] }
. Details are explained in the next section.
Example
Function.prototype.toString()
now returns exact slices of source code text, including whitespace and comments. Here’s an example comparing the old and the new behaviour:
Checkout more details from: TC39 proposal
Before you say goodbye…
Thank you so much for taking your precious time to read this post.
Reach me @LinkedIn
Previous post:
ECMAScript ES7 & ES8 New Features -JavaScript
ECMAScript 2018 (ES2018 or ES9) New Features -JavaScript
Stay tuned for ES11(ES2020) update…..