Destructuring and spread

Pull values out of objects and arrays, then copy and merge them without mutation.

Beginner 12 min read Lesson 8 of 480 Challenge included
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.

modern.jsJavaScript
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");
SyntaxMeaning
const { a, b } = objRead named properties
const [first, ...rest] = listTake the first item and the rest
{ ...obj, key: value }Copy then override
obj?.a?.bStop safely when a value is nullish
value ?? fallbackFallback only for null or undefined
Your turn

Build a learner summary

Easy

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 here
Check 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 in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 8 of 480 · 0% of course