What’s new in JavaScript ES10 (ES2019)?
ES which stands for ECMAScript is a standardized specification of a scripting language. Initially it was named as Mocha and finally as JavaScript. So far there are ten editions published.
Get Ovindi Jayawardana’s stories in your inbox
Join Medium for free to get updates from this writer.
The new features of ES10 includes flat(), flatMap(), object.fromEntries(), trimStart(), optional catch binding and many more. In this post, we will take a look at these new features and see how they upgrade the language.
1. flat() and flatMap()
The flat() method flattens the array by a given depth. The default flat level is 1. The flat(Infinity) method flattens the array to the last depth of the array.
const Numbers = [12,3,3,[33,3,4,55,[34,6,7,9,0]]];
console.log(Numbers.flat()); // Default flat level is 1
// output : [12, 3, 3, 33, 3, 4, 55, Array(5)]
console.log(Numbers.flat(Infinity));
// output : [12, 3, 3, 33, 3, 4, 55, 34, 6, 7, 9, 0]The flatMap() method first maps each element using a mapping function and then, flattens the result into a new array.
const Numbers = [1,2,3,4,5,6];
const alphaNumber = ['one', 'two', 'three', 'four', 'five', 'six'];
const mappedAndFlattenedExample = Numbers.flatMap((number, index) => [
number,
alphaNumber[index]
]);
console.log(mappedAndFlattenedExample);
// output : [1, 'one', 2, 'two', 3, 'three', 4, 'four', 5, 'five', 6, 'six']2. trimStart() and trimEnd()
The trimStart() / trimLeft() methods removes whitespaces from the beginning of the string.
const greeting = ' Hello world! ';console.log(greeting);
//output: " Hello world! ";console.log(greeting.trimStart());
//output: "Hello world! ";
The trimEnd() / trimRight() methods remove whitespaces from the end of the string.
const greeting = ' Hello world! ';console.log(greeting);
//output: " Hello world! ";console.log(greeting.trimEnd());
//output: " Hello world!";
3. Object.fromEntries()
The Object.fromEntries() method transforms a list of key-value pairs into an object.
const entries = new Map([
['foo', 'bar'],
['baz', 42]
]);const obj = Object.fromEntries(entries);console.log(obj);
//output: Object { foo: "bar", baz: 42 }
4. Optional Catch Binding
One of the new features they came up with is the ability to not to bind the error parameter within the catch close.
try {
throw "Error not bothered about";
} catch {
// logic..
}5. Description property for Symbol objects
When creating symbols, now you can add a description to it for debugging purposes. It’ll return a string containing the description of the symbol.
let sym = Symbol('foo');
console.log(sym.description);
//output: foo
sym = Symbol();
console.log(sym.description);
//output: undefined
// create a global symbol
sym = Symbol.for('bar');
console.log(sym.description);
//output: bar



