DEV Community

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

Posted on

4 1

[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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →