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 288 views
1 answer 283 views
1 answer 230 views
2 answers 293 views
1 answer 350 views
1 answer 274 views
...