Swapping variables is simple enough, but can it be any simpler?
Consider the below code -
let x = 1;
let y = 3;
To swap values -
let z = x;
x = y;
y = z;
console.log(x, y);
// 3 1
Applying our earlier knowledge about destructuring objects(/destructuring-assignments-in-javascript/), we can instead do -
let x = 1;
let y = 3;
[x, y] = [y, x];
console.log(x, y);
// 3 1
Beautiful, isn’t it?