Daily Software Tips & Tricks

Bite-sized knowledge to improve your coding skills daily.

The Cleanest Way to Swap Variables in JavaScript

February 2, 2026

Forget the old, messy three-line temporary variable dance when you need to swap the values of two variables, say `a` and `b`. Modern JavaScript provides an incredibly clean, readable way using array destructuring. Simply write `[a, b] = [b, a]`. The right side creates a temporary array with the values in the swapped order, and the left side immediately assigns those values back to the original variables. It's concise, effective, and makes your code much easier to read during a quick review.