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 285 views
1 answer 280 views
1 answer 227 views
2 answers 290 views
1 answer 343 views
1 answer 273 views
...