Rest and Spread

The ...rest and ...spread Operators

In an array literal or a function call, ... is spread: it expands an iterable into elements or arguments. In a destructuring pattern or parameter list it is rest: it gathers the remaining values into a new array. Object spread and rest (Object Destructuring) copy own enumerable properties instead of iterating.

Spread and rest on arrays, plus a recursive tree flattenerJavaScriptLive
const nums = [...new Set([3, 1, 3, 2])];          // spread an iterable: dedupe
const [head, ...tail] = nums;                     // rest collects the remainder
console.log(nums, head, tail, Math.max(...nums), [...'hi', ...tail]);
const tree = [
  { id: 1, children: [] },
  { id: 2, children: [{ id: 3, children: [{ id: 4, children: [] }] }] },
];
const flatten = ({ children, ...node }) => [node, ...children.flatMap(flatten)]; // rest, spread
console.log(tree.flatMap(flatten));