Today I Learned
Tidbits of (hopefully) useful information on technologies and tools related to software development.

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
Want to improve this post? Feel free to submit a pull request!

Learnings sourced by everyone who has taught me, either directly or indirectly. Thanks!

Visit my main site for more, and sometimes longer, content.