Fundamentals 5 min read

Node.js 10‑12 Tricks: Optional Catch, Trim, Symbol Description, Array Flat, JSON Fix

This article demonstrates several modern Node.js capabilities—including optional catch bindings without parameters, whitespace trimming methods, accessing Symbol description strings, flattening arrays with flat and flatMap, handling Unicode in JSON serialization, and converting entry lists to objects with Object.fromEntries—providing concise code examples for each feature.

Node Underground
Node Underground
Node Underground
Node.js 10‑12 Tricks: Optional Catch, Trim, Symbol Description, Array Flat, JSON Fix

Optional catch binding

Supported in Node 10+

Example of parsing JSON with try/catch, showing that the catch parameter can be omitted.

let parseResult = someString;
try {
  parseResult = JSON.parse(parseResult);
} catch (unused) {
}

With this feature, the catch binding can omit the parameter and parentheses:

let parseResult = someString;
try {
  parseResult = JSON.parse(parseResult);
} catch {
}

String whitespace removal

Supported in Node 10+

Remove leading spaces

const str = "   Node 地下铁!   ";
str.trimStart(); // or str.trimLeft()

Remove trailing spaces

const str = "   Node 地下铁!   ";
str.trimEnd(); // or str.trimRight()

Symbol description string

Supported in Node 11+

Symbol values are unique; toString() returns a string like "Symbol(123)". Direct concatenation throws a TypeError. The new description property returns the description without the wrapper.

Symbol(123).toString(); // "Symbol(123)"
// Symbol(123) + "test" -> TypeError
Symbol(123).description; // "123"
Symbol(123).description + "test"; // "123test"

Array flattening

Supported in Node 11+

The flat method merges nested arrays up to a given depth (default 1).

const arr = ["N", ["o","d"], "e", ["地","下"], "铁"];
arr.flat(); // ["N","o","d","e","地","下","铁"]

const arr2 = ["N", ["o",["d",["e",["地",["下",["铁"]]]]]]];
arr2.flat(6); // ["N","o","d","e","地","下","铁"]

The flatMap method applies a function then flattens one level.

const arr = ["Node","", "地下铁"];
arr.map(s => s.split("")); // [["N","o","d","e"],[],["地","下","铁"]]
arr.flatMap(s => s.split("")); // ["N","o","d","e","地","下","铁"]

More compliant JSON

Supported in Node 12+

JSON.stringify should correctly escape Unicode surrogate pairs.

JSON.stringify('𝌆') === JSON.stringify('\uD834\uDF06'); // true
// Expected escaped form: "\\uD834\\uDF06"

Object.fromEntries

Supported in Node 12+

Converts a list of key‑value pairs into an object.

const obj = Object.fromEntries([
  ["name", "test"],
  ["value", "testValue"]
]);
// { name: "test", value: "testValue" }
Node.jsSymbolES2020array flatcatch bindingObject.fromEntriesstring trim
Node Underground
Written by

Node Underground

No language is immortal—Node.js isn’t either—but thoughtful reflection is priceless. This underground community for Node.js enthusiasts was started by Taobao’s Front‑End Team (FED) to share our original insights and viewpoints from working with Node.js. Follow us. BTW, we’re hiring.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.