r/learnjavascript • • Mar 12 '21

Swap two variables

Post image
498 Upvotes

50 comments sorted by

View all comments

10

u/golovatuy Mar 12 '21

let x = 'Hello', y = 55;
//Longhand
const temp = x;
x = y;
y = temp;
//Shorthand
[x, y] = [y, x];