How to swap two numbers in JavaScript

1 Answer

0 votes
let x = 12;
let y = 9;
 
console.log("x = " + x + " y = " + y); 
 
[x, y] = [y, x];
 
console.log("x = " + x + " y = " + y); 
 
 
 
 
/*
run:
 
"x = 12 y = 9"
"x = 9 y = 12"
 
*/

 



answered Nov 6, 2022 by avibootz

Related questions

1 answer 173 views
6 answers 490 views
1 answer 123 views
3 answers 232 views
1 answer 188 views
1 answer 112 views
...