function swap(x,y){
var t=x;
x=y;
y=t;
}
This won't work. when you swap(a,b)
, variable a
and b
get copied into the function and whatever happens in that function doesn't affect the real value of a
and b
. I want something like this:
(function(){
a=1;
b=2;
function swap(){//something}
swap(a,b);
console.log(a) //2
console.log(b) //1
})()
How to do this?