+ 3
How to swap two variables in JavaScript?? i want swap() function!
var a = 10, var b 5; swap(a,b); console.log(a, b) // a = 5, b = 10
3 ответов
+ 4
es6 style
[a,b]=[b,a]
or with a binary operator
a = a ^ b;
b = a ^ b;
a = a ^ b;
or with a temporary variable
var tmp;
tmp = a;
a = b;
b = tmp;
+ 3
function swap(x,y) {
a = y;
b = x;
// supposing that a and b are global variables
}
0
use temporary variable
var temp;
temp=a;
a=b;
b=temp;