Destructuring Without Declaration
You'll learn how to destructure values into already-declared variables by wrapping the pattern in parentheses.
The Core Problem
So far, you've always destructured values at the moment of declaring new variables:
const { name, age } = person;
But what if name and age already exist? What if you want to reassign them through destructuring instead of creating new bindings?
The Parentheses Solution
When destructuring into existing variables, JavaScript requires you to wrap the entire destructuring pattern in parentheses:
let name, age;
// Later, destructure into those existing variables:
({ name, age } = person);
Why the parentheses? Without them, JavaScript sees { and thinks you're starting a code block, not an object pattern. The parentheses tell the parser: "This is an expression, not a block."
Array Destructuring Works Too
Arrays don't have the ambiguity problem (no curly braces), but you can still destructure into existing variables:
let first, second;
[first, second] = [10, 20]; // No parentheses needed for arrays
For consistency and clarity, some developers use parentheses anyway: ([first, second] = [10, 20]).
Practical Use Case
This is especially useful when you want to update variables conditionally or in loops:
let x, y;
({ x, y } = getCoordinates()); // Reassign based on function result
Key Takeaway: Use parentheses ({ prop } = obj) when destructuring into existing variables with objects to avoid syntax errors; arrays work without them but accept parentheses for consistency.