Data Structures & Modern Syntax · 9 min read · Beginner
Destructuring, Spread & Optional Chaining
Write concise, crash-proof JavaScript with object/array destructuring, immutable spread copies (...), and optional chaining (?.).
What you'll learn
- Extract properties and array items cleanly with destructuring
- Clone and merge objects or arrays immutably with the spread operator (...)
- Read deeply nested properties safely using optional chaining (?.)
These three modern ES syntax features make your code dramatically shorter and safer:
- Destructuring:
const { title, level = 'Beginner' } = course; - Spread (
...):const updated = { ...course, completed: true };(copies without mutatingcourse!) - Optional Chaining (
?.):const city = user?.profile?.address?.city ?? 'Unknown';(returnsundefinedinstead of throwingTypeError: Cannot read properties of undefined!)
Fill-in-the-Blank Exercise
Destructure, Spread & Optional Chain
Destructure title from course, clone tags with a new item, and safely read author.social.github.
const { } = course;
const allTags = [tags, 'modern'];
const handle = course.authorsocialgithub ?? 'none';Try It Yourself
Destructuring, Spread & Optional Chaining
Live Result
Console Output
No console output yet. Try calling console.log(...) in JS.
Check Your Understanding
Score: 0 / 1
1. What does `user?.settings?.theme` return if `user.settings` is undefined?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.