freecoding.school100% FREE · NO SIGNUP
Closure CavesISSUE #15 of 61

spread on objects · merging

LambdaVSThe Mutator
Lambda saysObject spread {...obj} shallow-copies and merges objects concisely.

{ ...a, ...b } builds a new object with all of a's then b's properties — later keys win, so it's perfect for overriding defaults ({ ...defaults, ...overrides }). It's a shallow copy: nested objects are shared, not cloned. The modern replacement for most Object.assign uses.

Power-ups you unlock

The Mutator attacks — common mistakes

Boss battleMerge a defaults object with overrides using spread so overrides win.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>const defaults={theme:'dark',size:'m'}; const opts={...defaults, size:'l'};
document.getElementById('o').textContent=JSON.stringify(opts);</script></body></html>
▶ Open the interactive comic issue
‹ Object Methods · Assign · Freeze · SealComputed Property Names ›