Free JavaScript reference · Python timeline
JavaScript ES2019: Shape collections without extra bookkeeping
Array flattening, object construction from entries, and optional catch bindings simplify common transformations.
Edition 10 · Published · Editorial review October 26, 2026
Standardized in ES2019 · Built-in method
flat() and flatMap() flatten one level
flat() returns an array with a chosen nesting depth removed; its default depth is one. flatMap() maps values and flattens one level of the produced arrays. It is useful when one input should produce zero, one, or several outputs.
Neither operation means recursive flattening by default. Nested values beyond the selected depth remain nested, and objects stored as elements are still shared.
The example uses an empty array to omit a negative value, and a two-element array to expand each accepted value. This keeps filtering and expansion in one transformation.
Worked example
Uses ES2019 features · Script
console.log(JSON.stringify([[1], [2, [3]]].flat())); const expanded = [2, -1, 3].flatMap(n => n < 0 ? [] : [n, n * 2]); console.log(JSON.stringify(expanded));
Expected output
[1,2,[3]]
[2,4,3,6]
Example checked with Chromium 151.0.7922.34, Node.js 24.19.0.
Common mistake
flatMap only flattens one level and is not an asynchronous map operation. Promise results are not automatically awaited.
Try it yourself
Return [[n]] from the callback and predict what remains nested.
Read more: ES2019 specification · MDN explanation and compatibility
Standardized in ES2019 · Built-in method
Object.fromEntries() builds a record
Object.fromEntries() turns an iterable of key-value pairs into an object. Combined with Object.entries, it supports transformations such as converting a price table while keeping named fields.
Duplicate keys are resolved by later entries. The resulting object is a record with property keys, so it does not preserve every distinction that arbitrary Map keys can represent.
The example uses string keys and integers to avoid rounding concerns. The original entries remain available; the transformation returns a new object.
Worked example
Uses ES2019 features · Script
const pairs = [["Ada", 8], ["Mina", 9]]; const doubled = Object.fromEntries(pairs.map(([name, score]) => [name, score * 2])); console.log(JSON.stringify(doubled));
Expected output
{"Ada":16,"Mina":18}
Example checked with Chromium 151.0.7922.34, Node.js 24.19.0.
Common mistake
A later duplicate key silently replaces an earlier one. Do not use this operation when collisions should be reported as invalid data.
Try it yourself
Add another Ada entry. Predict the final value, then describe how you would detect duplicates before construction.
Read more: ES2019 specification · MDN explanation and compatibility
Standardized in ES2019 · Syntax
Catch without an unused binding
A catch clause can omit its binding when the error value is not needed. This is useful for a narrow operation where the fallback is the same for every expected failure.
The decision to catch is still part of the program's contract. Omitting a name does not justify wrapping unrelated work in a broad catch or ignoring failures that deserve reporting.
The example catches an invalid JSON document and prints a deliberate fallback. In a larger application, distinguish parse failure from later validation and processing failures.
Worked example
Uses ES2019 features · Script
try { JSON.parse("not JSON"); } catch { console.log("invalid document"); }
Expected output
invalid document
Example checked with Chromium 151.0.7922.34, Node.js 24.19.0.
Common mistake
A broad catch can hide a programming defect as though it were expected bad input. Keep the guarded operation small.
Try it yourself
Add a valid parse followed by a deliberately broken operation. Which catch boundary would preserve useful diagnostics?
Read more: ES2019 specification · MDN explanation and compatibility
Recap
Array flattening, object construction from entries, and optional catch bindings simplify common transformations. Check the engines your project supports before adopting new syntax or built-ins.
Read the ES2019 specification →