JavaScript theorytheory 0/50 · 0%
Syntax · medium

22. Destructuring and spread

Pulling values apart and copying them back together.

Destructuring extracts values with defaults and renaming; spread copies enumerable own properties one level deep.

const { name, chainId: id = 1 } = net;
const [first, ...rest] = txs;
const merged = { ...defaults, ...overrides };

Spread is a shallow copy: nested objects are still shared references.

Check your understanding

  1. 1. What does `{ chainId: id }` do in destructuring?

  2. 2. How deep is a spread copy?

  3. 3. In `{...a, ...b}` which wins on conflict?