Lesson 12 of 20
Spread & Rest
Copy, combine and collect values.
Introduction
Spread (...) expands an array or object. Rest uses the same syntax to collect remaining values into an array.
Spread helps create immutable updates instead of changing an existing object directly.
Key insight
Copy, combine and collect values.
Example
const first = [1, 2];
const all = [...first, 3];
console.log(all);This example shows the core spread & rest idea in a small, runnable program.
Real-world example
State updates in React commonly use object and array spread.
Test yourself
2-question check
1
What is the main idea in Spread & Rest?
2
Where can spread & rest be useful?
Ready to check your understanding?
Answer every question, then submit your quiz.
Try it yourself
JavaScript compiler
Runs in your browser. Nothing is sent to a server.
Output appears here
Practice challenge
Combine two arrays without mutating either one.