Hemanth.HM

A Computer Polyglot, CLI + WEB ♥'r.

ES2019 Features

| Comments

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
4
5
[1, 2, 3].flatMap((x) => [x, x * 2]);
// => [1, 2, 2, 4, 3, 6]

[1, [2, [3]]].flat(Infinity);
// => [1, 2, 3]

Object.fromEntries

1
2
3
4
5
6
const iterableOfEntries = new Map([
    ['cat', 'dog'],
    ['life', 42]
]);
const obj = Object.fromEntries(iterableOfEntries);
console.log(obj); // { cat: "dog", life: 42 }

String#{trimStart,trimEnd}

1
2
"    Hey JS!".trimStart(); // "Hey JS!"
    "Hey JS!    ".trimEnd(); // "Hey JS!"

Symbol#description

1
2
3
const symbol = Symbol('TC39');
console.log(symbol.description); // 'TC39'
console.log(symbol.hasOwnProperty('description')); // false

try { } catch {} // optional binding

1
2
3
4
5
try {
    throw new Error("End of life!");
} catch { // ✋
    console.log("^ no params for catch, you are dead anyway!");
}

JSON ⊂ ECMAScript

1
2
3
// Without the proposal:
"foo<U+2028>bar<U+2029>baz"
// → SyntaxError
1
2
3
// With the proposal:
"foo<U+2028>bar<U+2029>baz"
// does not throw

well-formed JSON.stringify

1
2
JSON.stringify('\uD800');
// → '"\\ud800"'

Function#toString

1
2
3
4
5
function /* this is bar */ bar () {}

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

// ^ perviously this was not the case.

Array#sort stability

1
2
3
4
5
6
7
8
9
10
11
12
[
  { 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.

Don't miss:

Comments