Sub topic 12
Assigning to Existing Variables and Swapping Values
This chapter assumes you already know array and object destructuring from earlier chapters and know how let differs from const. So far, every pattern has appeared in a declaration such as const [a, b] = .... Patterns also work in plain assignments. A plain assignment updates variables that already exist.
Destructuring Without a Declaration
If you leave out let or const, the pattern assigns to existing bindings. The bindings must be reassignable, so they need to be declared with let, not const.
function minMax(nums) {
return [Math.min(...nums), Math.max(...nums)];
}
let low;
let high;
[low, high] = minMax([4, 9, 1, 7]);
console.log(low, high); // 1 9A function can return several values in one array, and the caller can unpack them into variables that were declared earlier, perhaps outside a loop.
Swapping Without a Temporary Variable
The classic use is swapping two values:
let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a, b); // 2 1The swap works because JavaScript evaluates the right-hand side first. The array literal [b, a] captures the current values [2, 1] before either variable changes. The pattern on the left then assigns those values by position.
The same trick swaps array elements in place, which is handy in sorting algorithms:
const items = ['x', 'y', 'z'];
[items[0], items[2]] = [items[2], items[0]];
console.log(items); // ['z', 'y', 'x']Any Assignment Target Works
In an assignment pattern, each target can be anything that may appear on the left of =. That includes variables, object properties, and array elements. Declarations allow only plain variable names, so this flexibility belongs to assignments alone.
const point = {};
({ x: point.x, y: point.y } = { x: 3, y: 4 });
console.log(point); // { x: 3, y: 4 }The Parentheses Rule for Object Patterns
A statement that begins with { is parsed as a block. To use an object pattern as a statement, wrap the whole assignment in parentheses:
let x;
({ x } = { x: 10 });
console.log(x); // 10Without the parentheses, { x } = { x: 10 }; is a SyntaxError. Array patterns don't need parentheses because [ cannot start a block.
Common Errors
| Mistake | Result | Fix |
|---|---|---|
| Assigning to a `const` binding | `TypeError: Assignment to constant variable` | Declare with `let` |
| Object pattern without parentheses | `SyntaxError` | Wrap in `( ... )` |
| Missing semicolon before `[` | The line is misparsed | Add `;` |
Exercise
Given let first = 'A', second = 'B', third = 'C';, write one statement that rotates the values so that first is 'B', second is 'C', and third is 'A'.
Show answer
[first, second, third] = [second, third, first]; The right-hand array is built from the old values ['B', 'C', 'A'] before any variable changes, and those values are then assigned in order.
Exercise
Why does { a, b } = obj; fail as a statement while ({ a, b } = obj); works?
Show answer
A statement that begins with { is parsed as a block, so = obj becomes a syntax error. The parentheses force the parser to read { a, b } as an expression, which makes it an object assignment pattern.