DEV Community

Cover image for [a, b] = [b, a]
Imed Adel
Imed Adel

Posted on

[a, b] = [b, a]

Today I realized that I can easily swap values in JavaScript using destructuring 😲 I don't know how I never noticed this before!

let a = 1, b = 2
[a, b] = [b, a]
console.log({a, b}) //=> {a: 2, b: 1}

Top comments (0)