Destructuring and spread
Pull values out of objects and arrays, then copy and merge them without mutation.
What you will be able to do
- Destructure objects and arrays
- Use rest and spread
- Read nested values safely
Destructuring names the values you care about. Spread copies the rest into a new object or array, which keeps updates predictable.
const learner = { name: "Ada", scores: [88, 92], cohort: "A" };
const { name, scores, cohort: group = "none" } = learner;
const [first, ...rest] = scores;
console.log(name, group, first, rest);
const updated = { ...learner, cohort: "B" };
const merged = [...scores, 79];
console.log(updated.cohort, merged);
const email = learner?.contact?.email ?? "not provided";
console.log(email);
const settings = { theme: null };
console.log(settings.theme ?? "light");| Syntax | Meaning |
|---|---|
| const { a, b } = obj | Read named properties |
| const [first, ...rest] = list | Take the first item and the rest |
| { ...obj, key: value } | Copy then override |
| obj?.a?.b | Stop safely when a value is nullish |
| value ?? fallback | Fallback only for null or undefined |
Your turn
EasyBuild a learner summary
Create a new object with fullName and best score, without changing the input objects.
JAVASCRIPT · starter
const user = { first: "Ada", last: "Lovelace" };
const scores = [88, 92, 79];
// your code hereCheck your understanding before moving on.
Knowledge checkQuestion 1
What is the value of a in const { a = 5 } = {}?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.