Learning objective: Use spread syntax to copy and combine arrays and objects, pass array elements as arguments, and collect remaining values with rest syntax.
Spread (...) expands an array's items or an object's properties into a new array, object or argument list: [...a, ...b] joins two arrays.
Spread makes a new array or object, so changing the copy does not change the original's top level.
In objects, later properties win: { ...user, plan: "pro" } copies user and then replaces plan.
Rest uses the same dots on the receiving side to collect what is left: function sum(...numbers) gathers all arguments into an array, and const [first, ...others] = list; collects the remaining items.
Spread copies are shallow: nested objects and arrays are still shared. Use structuredClone when you need a deep copy of plain data.
A rest element must be last: const [...a, b] = list is a SyntaxError.
Math.max(...numbers) passes each element as an argument. With very large arrays (hundreds of thousands of items) this can exceed argument limits; reduce is safer there.
Object rest (const { password, ...safe } = user;) is a clean way to omit fields before sending or logging data.
05Definitions
Spread syntax
... used to expand an array or object into individual elements or properties.
Rest parameter
...name in a parameter list, collecting remaining arguments into an array.
Rest element
...name in a destructuring pattern, collecting the remaining items or properties.
Shallow copy
A new top-level array or object whose nested values are still shared with the original.
const settings = { theme: "light", alerts: true };
// TODO: create next as a copy of settings with theme "dark"
console.log(settings.theme, next.theme);
Merge tag lists
Use rest parameters and spread together.
// Write mergeTags(...lists)
09Prediction
Commit to an answer first, then run the real code to compare.
const a = { x: 1, y: 2 };
const b = { y: 9, ...a };
console.log(b.y);
What is printed?
10Debugging investigation
Undo history changes when the draft changes
The editor saves a snapshot before editing, but the snapshot changes too, so undo shows the edited tags.
Interface frameworks compare old and new state objects to decide what to redraw. Updating with spread produces a new object each time, so the change is easy to detect and the old state is kept for undo.
6. To add an item to a cart stored in state, which is safer?
state.cart.push(item)
state = { ...state, cart: [...state.cart, item] }
state.cart[state.cart.length] = item
delete state.cart
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Fix the nested array: Prints 3; day is flat
Toggle a setting immutably: Prints light dark; Keeps other settings; A new object
Merge tag lists: Prints unique tags; No lists gives []; Inputs unchanged
ex-042-01: output matches the documented expected output
ex-042-02: output matches the documented expected output
ex-042-03: output matches the documented expected output
ex-042-04: output matches the documented expected output
ex-042-05: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-02-core-development/module-09-modern-javascript-syntax/lesson-042-spread-and-rest-syntax/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
Spread expands arrays and objects into new ones.
Later object properties win.
Rest collects remaining arguments, items or properties.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationspread and rest syntax
Context: I am learning spread and rest syntax in JavaScript.
Task: Explain the difference between spread and rest with shopping cart examples.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionspread and rest syntax drill
Context: I have practised spread and rest syntax.
Task: Give me six snippets mixing spread, rest and property order and ask what each prints.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingspread and rest syntax bug
Context: My copy made with spread still changes the original.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationspread and rest syntax practice
Context: I finished the spread and rest syntax exercises.
Task: Create three exercises that update state immutably with spread.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewspread and rest syntax review
Context: Here is my code that uses spread and rest syntax.
Task: Review my code for mutations that spread could replace, and for shallow-copy mistakes.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.