What is the spread operator?
The spread operator... expands an array or object into its individual elements. It’s how you copy, merge, and build new data structures without mutating the originals.
Array spreading
Copying an array
Python mental model:
copy = [...original] is like copy = [*original] or copy = original.copy(). In all cases, this is a shallow copy.Merging arrays
Adding items without mutation
Object spreading
Copying an object
Merging objects
userPrefs.theme (“dark”) overrides defaults.theme (“light”).
Updating a property without mutation
Practical patterns
Building API request bodies
Removing a property (rest + spread)
password) with rest syntax (...safeUser to collect everything else). It’s a clean way to strip sensitive fields.
Conditional properties
The
...(condition && { props }) pattern conditionally includes properties. If the condition is false, false is spread (which adds nothing). You’ll see this when building request bodies with optional fields.Comparing to Python
- JavaScript
- Python
... is like Python’s * for lists and ** for dicts. Same concept, same patterns.
What’s next?
You can work with arrays and objects. Now let’s learn about JSON — the format your frontend and backend use to exchange data.JSON basics
Convert between JSON strings and JavaScript objects