Photo by RetroSupply on Unsplash
JavaScript 2019 - New Features
ECMAScript 2019(ES10) : New Capabilities
Array.flat() function
The flat() method concatenates all sub elements of an array and returns a new array.The programmer can identify the depth of sub elements. The default depth is 1.
// Array.flat
let nestedArray = [1, [2, [3]]];
const flattened = nestedArray.flat(2);
console.log(flattened); // [1, 2, 3]
let myArr = [[1, 2], [3, 4], [5, 6]];
let newArr = myArr.flat();
console.log(newArr);// Result: [1, 2, 3, 4, 5, 6]
In the above code nestedArray object is flattened at depth 2 and myarr object is flattened at depth 1.
Array.flatMap() function
It maps each element of the host array using a callback function and returns a new array object.
let myArr = [1, 2, 3, 4, 5, 6];
let newArr = myArr.flatMap((x) => x * 2);
console.log(newArr); // Output: [2, 4, 6, 8, 10, 12]
In the above code each element of the myarr object is doubled using callback function and a new array is returned.
Catch without parameter
The catch clause parameter is optional . This simplifies exception handling.
// catch without parameter
try {
throw 10;
}catch {
console.log("error");
}
In the above code the catch clause does not have error object a parameter as it is optional.
String.trimStart() and String.trimEnd() functions
Both of these functions are used for removing extra spaces from start and end of the string.
// string - trimStart, trimEnd
let text = ' Namaste, JavaScript! ';
let trimmedStart = text.trimStart();
const trimmedEnd = text.trimEnd();
console.log(trimmedStart); // Output: Namaste, JavaScript!
console.log(trimmedEnd); // Output: Namaste, JavaScript!
In the above code the extra space from text string is remove from starting and ending of the string.
Object.fromEntries() function
It is used for converting a list of k/v pair to an object. The list should have data in the key/value format.
// fromEntries - array to object - which has KV
const entries = [['a', 1], ['b', 2]];
const obj = Object.fromEntries(entries);
console.log(obj); // Output: { a: 1, b: 2 }
In the above code the 2D array is converted to an object with key/value pairs.
// map to object - fromEntries
const map = new Map([
["Lang", "JS"],
["lang2", "JS2"],
]);
let obj2 = Object.fromEntries(map);
console.log(obj2); // {Lang: 'JS', lang2: 'JS2'}
In the above code map object is converted to an object.
Symbol.description
The symbol object has a new description property which provides generic information about the Symbol object
// Symbol.description
let mySymbol = Symbol('My custom symbol');
console.log(mySymbol.description); // Output: 'My custom symbol'
In the above code the Symbol object's generic information can be got using description property.
Complete Code Listing
// Array.flat
let nestedArray = [1, [2, [3]]];
const flattened = nestedArray.flat(2);
console.log(flattened); // [1, 2, 3]
let myArr = [[1, 2], [3, 4], [5, 6]];
let newArr = myArr.flat();
console.log(newArr);// Result: [1, 2, 3, 4, 5, 6]
myArr = [1, 2, 3, 4, 5, 6];
newArr = myArr.flatMap((x) => x * 2);
console.log(newArr); // Output: [2, 4, 6, 8, 10, 12]
// catch without parameter
try {
throw 10;
}catch {
console.log("error");
}
// string - trimStart, trimEnd
let text = ' Namaste, JavaScript! ';
let trimmedStart = text.trimStart();
const trimmedEnd = text.trimEnd();
console.log(trimmedStart); // Output: Namaste, JavaScript!
console.log(trimmedEnd); // Output: Namaste, JavaScript!
// fromEntries - array to object - which has KV
const entries = [['a', 1], ['b', 2]];
const obj = Object.fromEntries(entries);
console.log(obj); // Output: { a: 1, b: 2 }
// map to object - fromEntries
const map = new Map([
["Lang", "JS"],
["lang2", "JS2"],
]);
let obj2 = Object.fromEntries(map);
console.log(obj2); // {Lang: 'JS', lang2: 'JS2'}
// Symbol.description
let mySymbol = Symbol('My custom symbol');
console.log(mySymbol.description); // Output: 'My custom symbol'