1
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?

shenkwen
  • 3,536
  • 5
  • 45
  • 85
  • 7
    In JS scalar variables are passed **by value**, not reference. So.. you _can't_. – hindmost May 10 '16 at 20:36
  • Possible duplicate of [How to swap two variables in JavaScript](http://stackoverflow.com/questions/16201656/how-to-swap-two-variables-in-javascript) – Vadzim May 10 '17 at 17:36
  • Possible duplicate of [swap() function for variables's values](https://stackoverflow.com/questions/40165189/swap-function-for-variabless-values) – Herohtar Jul 21 '19 at 20:20

5 Answers5

10

If you are using the latest version of JavaScript (ES2015), then you can use the new language feature called "destructuring". You don't need a function. It looks like this:

let a = 1;
let b = 2;

// swap!
[b, a] = [a, b];

If you want a function, you can do it like this:

function swap(a, b) {
  return [b, a]
}

[a, b] = swap(a, b);

Looking at this code, I kind of prefer the function, though it is a bit superfluous. I like how expressive it is. You aren't left puzzling over what the code does.

Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
3

You can't. Arguments are passed by value, even in the case of objects. It's just that the value passed for them is a reference to the object.

Basically this means that any arguments you receive have no actual connection to the original variables you passed in except that they may share a reference.

Imagine you've written a and b on a piece of paper. In JS, the only way to share those values with a function is to copy them on to a different piece of paper and hand it to the function. So even if that function scratches out a on the new piece of paper and replaces it with b, it doesn't change what you have written on the original piece of paper.

In short, it is not possible to change the value of a variable which was used as an argument for a function.

Community
  • 1
  • 1
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
0

you can set a variable outside the scope of the function so you can use it inside the function, this is something you can do:

<head>
<script>var x=0, y=1;</script>
</head>

<script>
function swap()
{
  var t = x;
  x = y;
  y = t;
} 
</script>

or even this works

<script>
var x=0; y=1;
function swap(id1, id2)
{
  var t = x;
  x = y;
  y = t;
} 
console.log(x+"  "+y);
</script>

I used this quite a lot and works fine. the x and y can be taken from any where and will work inside a function no problem. you can also do

function(id1, id2)
{
  x=document.getElementById(id1);
  y=document.getElementById(id2);

  var t = x.value;
  x.value = y.value;
  y.value = t;

}
bakz
  • 79
  • 15
  • The only problem with this is you can only swap those two variables. It doesn't allow you to swap any other variables. – Mike Cluck May 10 '16 at 20:49
  • what is it that you need exactly? you can set the x and y to be anything.. you can set hidden inputs with values or save the data you need inside an array, but passing the values to a a function and it makes changes to them is not possible. check my edit – bakz May 10 '16 at 20:51
  • What I mean is lets say I wanted to swap `x` and `y` at one point then swap `w` and `z` at a different point. This function doesn't let me do that. I can only swap those two variables instead of two arbitrary variables. – Mike Cluck May 10 '16 at 20:52
  • like i said, you can only work around it.. where do you get the x y and z data from? you can implement their values in the actual HTML, for example inside an array or in the ` – bakz May 10 '16 at 20:56
  • if you prove more information on the actual problem then I would be able to help you on a workaround, but trying to do it the way you asked about won't work. – bakz May 10 '16 at 21:03
  • I'm not OP. I was just pointing out a shortcoming of this approach. – Mike Cluck May 10 '16 at 21:04
0

As mentioned in the answers above, Arguments are only passed by value. If you really need to achieve the swap, you can use the 2 variables algorithm:

var a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
console.log (a+" "+b); // 20 10
achref
  • 1,150
  • 1
  • 12
  • 28
0
function swap(value) {
    value.first = value.first + value.second;
    value.second = value.first - value.second;
    value.first = value.first - value.second; 
}

// declared an object named value which has two keys - first and second corresponding to the first and the second values respectively

var value = {
    first: 1,
    second: 2
}

console.log(value.first, value.second) // prints 1 2
swap(value);
console.log(value.first, value.second); // prints 2 1