ES10 Interactive Hub — ECMAScript 2019
Quality-of-life upgrades: Array.flat/flatMap, Object.fromEntries, optional catch binding, trimStart/trimEnd, stable Array.sort, Symbol.description — many convenience methods for daily coding.
Array.flat() & flatMap()
Core topic
💡 Concept summary
flat() flattens nested arrays, flatMap() combines map + a single-level flat in one pass.
🤔 Why do you need this feature?
Before ES10, flattening an array required a clumsy reduce + concat or spread. Especially when a map produces arrays (e.g. splitting sentences into words), the result is a 2D array that needs an extra flat call.
// Before ES10 — manual flatten
var nested = [1, [2, 3], [4, [5, 6]]];
var flat = nested.reduce(function(acc, val) {
return acc.concat(val);
}, []);
console.log(flat); // [1, 2, 3, 4, [5, 6]] (only 1 level)
// map producing arrays → needs flattening
var sentences = ['hello world', 'goodbye'];
var words = sentences.map(function(s) { return s.split(' '); });
console.log(words); // [['hello','world'], ['goodbye']]// ES10 — flat() with a depth argument
const nested = [1, [2, 3], [4, [5, 6]]];
console.log(nested.flat()); // [1, 2, 3, 4, [5, 6]]
console.log(nested.flat(2)); // [1, 2, 3, 4, 5, 6]
console.log(nested.flat(Infinity)); // flatten infinitely deep
// flatMap — map + single-level flat
const sentences = ['hello world', 'goodbye'];
const words = sentences.flatMap(s => s.split(' '));
console.log(words); // ['hello', 'world', 'goodbye']ES10 Quick Reference
Quick syntax overview + copy snippets
flat() flattens nested arrays, flatMap() combines map + a single-level flat in one pass.
const nested = [1, [2, 3], [4, [5, 6]]]; console.log(nested.flat()); // [1, 2, 3, 4, [5, 6]] console.log(nested.flat(2)); // [1, 2, 3, 4, 5, 6] ...
The inverse of Object.entries() — converts an array of [key, value] pairs into an object.
const user = { name: 'Lan', age: 25, password: 'secret' };
const safe = Object.fromEntries(
Object.entries(user).filter(([k]) => k !== 'password')
...Lets you omit the error variable in catch when you do not need it — for more concise code.
function safeParse(str) {
try {
return JSON.parse(str);
...Removes whitespace only at the start or only at the end of a string — complementing the standard ES5 trim().
const str = " Hello ";
console.log(`[${str.trimStart()}]`); // [Hello ]
console.log(`[${str.trimEnd()}]`); // [ Hello]
...ES10 officially requires Array.prototype.sort to be STABLE — elements that are equal (per the comparator) keep their original order of appearance.
const users = [
{ name: "An", age: 25 },
{ name: "Bình", age: 25 },
...The "description" property lets you read a Symbol's description string directly instead of hacking it via String(sym).slice().
const sym = Symbol("userId");
console.log(sym.description); // 'userId'
const anon = Symbol();
...