What’s new in the ECMAScript 10 (2019)?
Nothing mindblowing to be fair
Many of us thought that some major improvements were gonna be made to ECMAScript with the release of the 10th version of the specification.
However, they were not. All the following proposals are still stage 3:
BigInt- Top-level
await Promise.allSettled()- Class accessors, fields and privacy modificators
And many other interesting ones.
Instead, editors decided not to rush and thus the 10th version of the specification only presents us with a few quite useful new methods and syntax/semantics improvements. Let’s dive into!
Array.prototype.flat()
Flattens a provided array, as it name suggests
[[["nested", ["arrays"]]], [["inside"]], [[["the"]]], ["array"]]
->
["nested", "arrays", "inside", "the", "array"]The method is not mutating
let a = [[1]];
a.flat(); // -> [1]
a; // -> [[1]]It accepts depth as its sole argument. The default value of depth is 1. The undefined value is also treated to be 1. All the other values are converted to integer (which is gonna evaluate to 0 most of the times)
let a = [[1]];a.flat(1); // -> [1]
a.flat(undefined); // -> [1]
a.flat("1"); // -> [1]let b = [[[2]]];b.flat(1); // -> [[2]]
b.flat(2); // -> [2]
b.flat("2"); // -> [2]
b.flat(100); // -> [2]
The good thing about the new flat method is that it’s finally presents. No need to implements it manually anymore. The bad thing is that there’s no way to handle arrays of unknown depth with it. You must either use something like Number.MAX_SAFE_INTEGER or recursively invoke the method each time checking whether the target array still contains nested arrays within itself.
Array.prototype.flatMap()
Get Matthew Mamonov’s stories in your inbox
Join Medium for free to get updates from this writer.
The name is a bit confusing though. It seems like it first flattens the input array and then maps it. But in fact it works exactly opposite to that: it first maps the array as it is and only then flattens it and returns the falttened version of the mapped array
let a = [[1], [[2]], [[[3]]]];a.flatMap((item, i) => item + i); // -> ["10", "21", "32"]
// but not [1, 3, 5] nor [1, "21", "32"] as you might've expected at first
If the provided callback is not callable (i.e. not a function), the TypeError error will be thrown (the same applies to the Array.prototype.map() function as well).
There’s no way to specify which depth you need to flatten the array to. The depth is slways 1.
Object.fromEntries
Object.entries returns an array of arrays composed of key-value pairs of the provided object
let a = {
a: 1,
b: 2
};Object.entries(a); // -> [["a", 1], ["b", 2]]
Object.fromEntries is essentially opposite to Object.entries — it creates an object from these key-value pairs (entries)
let a = [
["a", 1],
["b", 2]
];Object.fromEntries(a); // -> {a: 1, b: 2}
These 2 methods combined can become quite useful to copy objects
let a = {
a: 1,
b: 2
};Object.fromEntries(Object.entries(a)) === a; // -> false
// a new object is created
String.prototype.trimStart() and String.prototype.trimEnd()
… which are formerly known as trimLeft and trimRight respectively (though these 2 were non-standard and now are added to the specification with the backwards compatibility in mind).
As their names suggest, theString.prototype.trimStart() trims a target string’s start and the String.prototype.trimEnd() trims a target string’s end.
" Hello ".trim(); // -> "Hello"
" Hello ".trimStart(); // -> "Hello "
" Hello ".trimEnd(); // -> " Hello"There’re other minor syntax and semantics improvements as well.
It’s now legal to omit the catch binding parameters
try {
throw new Error();
} catch { // notice the absence of `()`
console.error(“An unknown error occurred”);
}Array.prototype.sort() has become “a stable sort”, meaning that if the comparison function returned 0, the initial elements will remain untouched. Previously the behavior was desided by implementations.
The JSON.stringify method now always returns UTF-8-encoded text regardless of what encoding its input was.
Implementations since now must return either the actual source text of a function when toString() is invoked on it or a standard placeholder.





