ES2019 features

hemanth.hm - Feb 7 '19 - - Dev Community

Cross post from h3manth.com.

So, it is time for an other post, this time it's ES2019 features.

Without wasting time with some placeholder content, the below are the features and examples:

Array#{flat,flatMap}

[1, 2, 3].flatMap((x) => [x, x * 2]);
// => [1, 2, 2, 4, 3, 6]

[1, [2, [3]]].flat(Infinity);
// => [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

Object.fromEntries

const iterableOfEntries = new Map([
    ['cat', 'dog'],
    ['life', 42]
]);
const obj = Object.fromEntries(iterableOfEntries);
console.log(obj); // { cat: "dog", life: 42 }
Enter fullscreen mode Exit fullscreen mode

String#{trimStart,trimEnd}

"    Hey JS!".trimStart(); // "Hey JS!"
    "Hey JS!    ".trimEnd(); // "Hey JS!"
Enter fullscreen mode Exit fullscreen mode

Symbol#description

const symbol = Symbol('TC39');
console.log(symbol.description); // 'TC39'
console.log(symbol.hasOwnProperty('description')); // false
Enter fullscreen mode Exit fullscreen mode

try { } catch {} // optional binding

try {
    throw new Error("End of life!");
} catch { // ✋
    console.log("^ no params for catch, you are dead anyway!");
}
Enter fullscreen mode Exit fullscreen mode

JSON ⊂ ECMAScript

// extend ECMA-262 syntax into a superset of JSON.
const LS = "";
const PS = eval("'\u2029'");
Enter fullscreen mode Exit fullscreen mode

well-formed JSON.stringify

JSON.stringify('\uD800');
// → '"\\ud800"'
Enter fullscreen mode Exit fullscreen mode

Function#toString

function /* this is bar */ bar () {}

bar.toString(); // 'function /* this is bar */ bar () {}'

// ^ perviously this was not the case.
Enter fullscreen mode Exit fullscreen mode

Array#sort stability

[
    { name: "Jan",     age: 20 },
    { name: "Jhon",    age: 20 },
    { name: "David",   age: 18 },
    { name: "Ram",     age: 18 },
    { name: "Sita",    age: 18 },
    { name: "Ravan",   age: 18 },
    { name: "Asura",   age: 12 },
    { name: "Milly",   age: 12 },
].sort((m, n) => m.age - n.age));

// People with the same age retain their order.

Enter fullscreen mode Exit fullscreen mode

Don't miss:


🎉 New JavaScript features in ES2019:

➡️ Array#{flat,flatMap}
➡️ Object.fromEntries
➡️ String#{trimStart,trimEnd}
➡️ Symbol#description
➡️ try { } catch {} // optional binding
➡️ JSON ⊂ ECMAScript
➡️ well-formed JSON.stringify
➡️ stable Array#sort
➡️ revised Function#toString

— Mathias Bynens (@mathias ) January 29, 2019
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .