JavaScript ECMAScript 2024 ·
✓ verified by execution on 2026-08-14
When building applications, you’ll constantly need to copy data, update state immutably, or combine multiple objects together. JavaScript makes this easy using the object spread syntax (...).
The spread syntax takes an object and “spreads” its individual properties into a brand new object.
Spread is a shallow copy: the top-level properties are duplicated, but a nested object is shared — both the original and the copy still point at the same inner object.
As the diagram shows, copy.sub and original.sub both point to the exact same nested object in memory. Modifying one modifies the other!
Immutable Updates
Spread syntax shines when you need to add or update a property without mutating the original object—a common pattern in modern JavaScript frameworks.
By placing the new property score: 20after...state, you immutably generate a fresh state object with an updated score, while leaving the previous state completely intact.
Rest Properties
When combined with destructuring, the ... syntax is called the rest operator. While destructuring unpacks properties you ask for, the rest operator neatly packs everything else left over into a new object.
By extracting id, the remaining properties (name and age) are bundled into the new details object.
Safe Spreading
If you try to destructure null or undefined, JavaScript throws a TypeError. However, spreadingnull or undefined into an object literal is completely safe!
Predict the outputjavascript
Read the code. What exactly will it print? Commit to an answer before you look.
const data = null;const safeCopy = { ...data };console.log(Object.keys(safeCopy).length);
Output
0
You predicted
It silently evaluates to an empty object without crashing your application.
Check yourself
When using the object spread syntax `{ ...objA, ...objB }` to merge two objects, what happens if both objects share the exact same property key?
Reveal answer
The property from the object that comes later overwrites the earlier one. — Object properties that appear later in the literal overwrite any earlier properties with the exact same key.
Is the object created by spread syntax a deep or shallow copy?
Reveal answer
It is a shallow copy; top-level properties are copied but nested objects are shared by reference. — Spread syntax only copies one level deep. Any objects or arrays nested inside are shared by reference between the original and the new copy.
If you try to spread `null` into an object using `{ ...null }`, what is the result?
Reveal answer
It silently evaluates to an empty object `{}` without crashing. — Unlike destructuring, spreading `null` or `undefined` into an object literal is perfectly safe and just produces an empty object.
Challenges
Challenge 1 +25 XP
A developer is trying to load a system configuration by combining the `baseConfig` with the `envConfig`. However, they want the `envConfig` to override the defaults. Merge them correctly into `finalConfig`.
javascript
Test 1 — expects "localhost 3000\n"
Need a hint? (−25% XP)
Use `{ ...baseConfig, ...envConfig }` to merge the configurations, ensuring envConfig comes last.
This developer is trying to pull the `password` out of the user object so they can send the remaining safe properties to the client. But they put the rest operator in the wrong place! Fix the syntax error so `password` is extracted and the rest go into `safeUser`.
This code runs. It just does the wrong thing. Read it, find the defect,
fix it — the tests below decide when you are right.
javascript
Test 1 — expects "admin true undefined\n"
Need a hint? (−25% XP)
The rest operator `...` must always be the very last element in the destructuring assignment.