Why destructuring?
Destructuring lets you unpack values from objects and arrays into separate variables in a single line. Instead of writinguser.name, user.email, user.role over and over, you extract them once.
Object destructuring
Pull specific properties out of an object by name:Python mental model: object destructuring is like doing multiple
dict lookups and assignments in one step (name = user["name"], email = user["email"]) — but with a much shorter syntax.Renaming variables
When the property name conflicts with an existing variable or you want a clearer name:Default values
Set fallbacks for properties that might not exist:Nested destructuring
Array destructuring
Pull values out by position:Python mental model: this is like tuple unpacking (
first, second = values) — but JavaScript adds useful extras like skipping items and collecting “the rest” with ...remaining.Skipping items
Swapping variables
Where you’ll use destructuring every day
React’s useState
You’ve already seen this — it’s array destructuring:useState returns a two-element array: [currentValue, setterFunction]. Destructuring gives them meaningful names.
React component props
Function parameters
API responses
Comparing to Python
- JavaScript
- Python
What’s next?
Destructuring unpacks values out of objects and arrays. The spread operator does the opposite — it expands them into new structures.Spread operator
Copy, merge, and expand arrays and objects