I wrote about new features coming into JavaScript yesterday, but there’s even more to talk about! Let’s take a look at them! You can run these examples in node.js ≥12 so if you want to experiment with these techniques you can!

Array.prototype.flat && Array.prototype.flatMap

The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

let array1 = ['a','b', [1, 2, 3]];
let array2 = array1.flat();
//['a', 'b', 1, 2, 3]

Be aware that the method excludes gaps or empty elements in the array:

let array1 = ['a', 'b', , , , 'c'];
let array2 = array1.flat();
// ['a','b','c']

The flatMap() method has the same effect as using the map() method followed by the flat() method with the default depth of 1.

let array1 = [1, 2, 3, 4];array1.flatMap(x => [x + 1]);
// [2, 3, 4, 5]

Object.fromEntries()

Object.fromEntries() creates an object or transforms key-value pairs into an object. It only accepts iterables.

const obj = { a: 1, b: 2, c: 3};const entries = Object.entries(obj);
// entries
(3) [Array(2), Array(2), Array(2)]
0: (2) ["a", 1]
1: (2) ["b", 2]
2: (2) ["c", 3]const obj2 = Object.fromEntries(entries)
// {a: 1, b: 2, c: 3}

String.trimStart() & String.trimEnd()

The trimStart()/trimEnd() methods removes white space from the beginning/end of a string.

let result = '      hello!'.trimStart();
// "hello!"let cadena = 'hello      '.trimEnd()
// "hello!"

Optional Catch Binding

It allows us to use try/catch without having supplied the error parameter within the catch block.

Without optional catch binding:

try {
    // do something
} catch (e) {
    // we have error parameter to handle it
}

With optional catch binding:

try {
    // do something
} catch {
    // no binding or parameter to handle it
}

It is not a recommended practice in most cases, however it makes sense when you know you’re not going to use the exception object.

Function “toString” Revision

The toString method must return a source text slice from the beginning of the first token to end of the last token matched by the appropriate grammar.

function /* kk*/ foo() { /* cc */ }// before:
foo.toString();
// "function foo() {}"// Now:
foo.toString();
//"function /* kk*/ foo() { /* cc */ }"

Array.Sort Stability

Previously, V8 used an unstable QuickSort for arrays with more than 10 elements. V8(Chrome ≥70) uses the stable TimSort algorithm.

const array1 = [
 { name: "a",   age: 14 },
 { name: "b", age: 14 },
 { name: "c",  age: 13 },
 { name: "d",   age: 13 },
 { name: "e",   age: 13 },
 { name: "f",    age: 13 },
 { name: "g",   age: 13 },
 { name: "h",  age: 13 },
 { name: "i",   age: 12 },
 { name: "j",   age: 12 },
 { name: "k",    age: 12 }
]const array2 = array1.sort( (a,b) => b.age - a.age)

The array is presorted alphabetically by name. To sort by age, we pass a custom callback that compares the ages. This is the result that you would expect:

0: {name: "a", age: 14}
1: {name: "b", age: 14}
2: {name: "c", age: 13}
3: {name: "d", age: 13}
4: {name: "e", age: 13}
5: {name: "f", age: 13}
6: {name: "g", age: 13}
7: {name: "h", age: 13}
8: {name: "i", age: 12}
9: {name: "j", age: 12}
10: {name: "k", age: 12}

The array is sorted by age, but within each age, they’re still sorted alphabetically by name.

Before, the JavaScript specification didn’t require sort stability for Array.sort and instead left it up to the implementation. And because of this, you could also get this sort of result where “b” appears before “a”:

0: {name: "b", age: 14}
1: {name: "a", age: 14}
2: {name: "c", age: 13}
3: {name: "d", age: 13}
4: {name: "e", age: 13}
5: {name: "f", age: 13}
6: {name: "g", age: 13}
7: {name: "h", age: 13}
8: {name: "i", age: 12}
9: {name: "j", age: 12}
10: {name: "k", age: 12}

Symbol.description

A symbol is a built-in datatype for unique identifiers. Now a new property Symbol.prototype.description is added to get a description from the symbol.

Before:

const symbol1 = Symbol('my symbol');
console.log(symbol1.toString());//Symbol(my symbol)

Now:

const symbol1 = Symbol('my symbol'); 
console.log(symbol1) 
console.log(String(symbol1) === `Symbol(${'my symbol'})`);
console.log(symbol1.description);//Symbol(my symbol)
//true
//my symbol

LEAVE A REPLY

Please enter your comment!
Please enter your name here