shenkwen
shenkwen

Reputation: 3880

javascript function to swap two variables

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?

Upvotes: 1

Views: 10625

Answers (5)

Anish Mandal
Anish Mandal

Reputation: 47

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

Upvotes: 0

Brian Genisio
Brian Genisio

Reputation: 48127

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.

Upvotes: 10

bakz
bakz

Reputation: 89

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;

}

Upvotes: 0

achref
achref

Reputation: 1220

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

Upvotes: 0

Mike Cluck
Mike Cluck

Reputation: 32511

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.

Upvotes: 3

Related Questions