Array Destructuring Swap
Multiple Choice Easy
Question
Can you swap variables with [a, b] = [b, a]?
Hint
Destructuring evaluates the right side first.
Explanation
[a, b] = [b, a] swaps the values of a and b. The right side [b, a] is evaluated first (creating a temporary array with the current values), then destructured into a and b. This is a clean, idiomatic way to swap variables without a temporary variable. It works with any types, not just numbers.
Related Resources
Related Lessons
- Arrays
Learn JS arrays.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.