Home > shorts > Swap Variables in JavaScript with destructuring assignment

Swap Variables in JavaScript with destructuring assignment


Destructuring assignment lets you extract items of an array into variables.

let x = 1;
let y = 2;

[x ,y] = [y, x]

console.log({ y, x })

// x: 2, y: 1

Check the demo here => CodePen

© 2021, Andrew Losseff