Swap JavaScript Variables in a Single Line
July 12, 2021 - JavaScript
With ES6, we can use array destructuring to swap variables in a single line.
let x = 1;
let y = 2;
[x, y] = [y, x];
console.log(x); // 2
console.log(y); // 1
July 12, 2021 - JavaScript
With ES6, we can use array destructuring to swap variables in a single line.
let x = 1;
let y = 2;
[x, y] = [y, x];
console.log(x); // 2
console.log(y); // 1