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 188 views
1 answer 157 views
1 answer 136 views
2 answers 183 views
1 answer 237 views
1 answer 172 views
...