How to swap two variables in JavaScript

1 Answer

0 votes
let x = 7;
let y = 3;

console.log(x, ":", y); 

[x, y] = [y, x];

console.log(x, ":", y); 



/*
run:

7:3
3:7

*/

 



answered Feb 12, 2021 by avibootz

Related questions

1 answer 196 views
1 answer 165 views
1 answer 143 views
2 answers 192 views
1 answer 247 views
1 answer 179 views
...