0

The actual function I provided in this example is not the problem. I just want to know how to save the values of variables after using them as parameters in functions. I was trying to make a very simple function that switches the value of two variables. I know how to do this without a function but wanted to generalize the solution with a function.

I tried writing the following code,

let a = 3;
let b = 4;

const switchItems = (c,d) => {
    var temp = c;
    c = d;
    d = temp;
}

switchItems(a,b);

I expected a to be 4 and b to be 3 but the variables stay the same as they were before the function.

I don't care how to do this with the specific swap function I used, I just want to know how to change a global variable inside a function without referencing it.

Ian Halstead
  • 88
  • 1
  • 9
  • 1
    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:15
  • 1
    Related: [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – Herohtar Jul 21 '19 at 20:16
  • You basically can't edit the variables value using only the arguments. you can return the swapped values: `{a, b} = swap(a,b);` – Hagai Wild Jul 21 '19 at 20:17
  • Why is this question being downvoted and being tagged to related posts? This is a valid question and a great learning experience for new JavaScript developers who search google for this type of question after coming from a language like C++. – Dalton Jul 21 '19 at 20:30
  • 1
    @Dalton It's being tagged with related posts because it has been asked before. A quick Google search brings up multiple questions about the same thing. The one I linked is essentially the same function written here, and it already has answers. – Herohtar Jul 21 '19 at 20:35
  • @Herohtar you have a point, and I don't disagree with that. However, a novice programmer on the web is not up to date with the intricacies of how Stack Overflow works. Someone, who does not know about pass by value and pass by reference will not be able to google search that question and find what they are looking for (in reference to the duplicate that answers this question). – Dalton Jul 21 '19 at 20:47
  • @Herohtar You are completly right. I am very new to using websites like stack overflow so this is all a little confusing to me, but I probably could have found an answer to my question by making a simple google search. – Ian Halstead Jul 21 '19 at 20:58

2 Answers2

1

You can't.

Javascript's calling semantics mean that the function receives a copy of the reference; if you change it, you're only changing the internal reference. The exception is that you can change values stored in an outer scope (such as the global scope.) But then the values you pass to it are irrelevant.

What you can do, if you like, is to return a swapped copy of the parameter array:

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

But there's no reason to create such a function, since this is less simple than

let [b, a] = [a, b];

Update

From the question edit:

I just want to know how to change a global variable inside a function without referencing it.

It depends upon what you mean by "change" here. You can assign it a new reference, but only if you have its name, and the parameters are then irrelevant. If you want to change its internals, that's simple:

const foo = {bar: 42}

function baz(x) {
  x.bar += 1;
}

baz(foo);
foo //=> {bar: 42}

But it seems you want something more than that, and that's not available.

Scott Sauyet
  • 44,568
  • 4
  • 43
  • 95
  • 1
    So just to clarify, there is now way to change the value of a global variable unless you specifically use it in the function? – Ian Halstead Jul 21 '19 at 20:21
  • That's overstating it. You can change the internals of a parameter or global variable (or anything else in scope) inside a function. And you can change the internal reference inside a function. But you cannot change the reference the calling code has. – Scott Sauyet Jul 21 '19 at 20:24
-1

TL;DR: It is possible, but you have to change how your variables are being passed in.

let numObject = {
  a: 2,
  b: 3
};

const swap = numObject => {
  const temp = numObject.a
  numObject.a = numObject.b;
  numObject.b = temp;
}

swap(numObject);

document.getElementById("answer").textContent = `A: ${numObject.a}, B: ${numObject.b}`
<div id="answer"></div>

Explanation: The method you used was pass by value, meaning that the value of the parameters you pass into the function are copied to the local scope of the function. This means that the function has no access to the original memory location of that variable, and is swapping the values of local variables. In JavaScript, both objects and arrays are pass by reference, meaning that the pointer to the memory location is passed to the function instead. That is why this is not possible when you use primitive variables like number.

Dalton
  • 477
  • 3
  • 10