1

How does passing by reference work when reassigning non-primitives in functions? When considering address'.

for example:

var a = [9]; //suppose this is stores at 0x01

function changeto5(array){ 
array = [5]; 
}

function(a);//passes the address 0x01
console.log(a) //outputs [9]

shouldn't the value at the memory address 0x01 be updated to [5], i'm very confused on why this does not happen. My current understanding of this, is that at the address 0x01 the value of [9] is stored, and a new address e.g 0x02 is used to store [5]. But this would mean there is no way to change the value stored at 0x01 unless done in the same scope and it seems inconsistent, as you can change the elements of the array in the function. Is my current understanding correct?

  • There is no "pass variable by reference" in javascript. "*passes the address 0x01*" does not happen. The only thing you can do is pass reference values - objects with mutable properties. – Bergi Dec 16 '22 at 03:38

2 Answers2

1

Your intuition is almost 100% correct.

  • Yes, you are instantiating a new array when you call [5]
  • To utilise the same array "at 0x01", You would need to use the array.pop() and array.push() functions.
  • Or you could clear the array by setting its length to 0 e.g array.length = 0
  • EDIT: Or you could access the element of the array directly as per spectric's answer below e.g: array[0] = 5

That way you do not instantiate a new array, but instead work with the existing array.

Tjad Clark
  • 552
  • 3
  • 17
  • So, a new local variable named array is being instantiated, which is only accessible within the function scope. And this local variable has a completely different address to 'a'. But, then how would you access the variable passed in after creating the new local variable, is it not possible? – Hasaan Khan Dec 16 '22 at 03:10
  • The variable will only be garbage collected when there are no strong references to the variable. So when a variable is instantiated in a function, but then assigned to (or referenced by) a variable outside the function scope, perhaps an argument to the function , e.g in this case an array, a new strong reference is created, hence the variable can be accessed outside the scope of the function and after the function exits. – Tjad Clark Dec 16 '22 at 03:15
  • Similarly if the function returns a variable it instantiated inside its scope and the return value is assigned in a variable, at that point the garbage collector knows not to free the memory. – Tjad Clark Dec 16 '22 at 03:17
  • sorry, is should have said how would you access the passed in variable inside the function after initializing a new variable under the same name. – Hasaan Khan Dec 16 '22 at 03:27
  • I think that is not possible. You would get an exception/error from the interpreter that the variable name(Identifier) has already been declared. i.e variable names have to be unique within each scope. declaring an argument is effectively adding an Identifier(variable name), so you can't declare a new variable with the same name – Tjad Clark Dec 16 '22 at 03:33
0

You are replacing the array in the function, which will only affect the array in the function's scope. The array outside the function still remains the same.

This is because non-primitives are not really passed by reference - the reference is instead copied, so under the hood, modifying the contents of the parameter will persist, but overwriting the "reference" won't affect the original.

Thus, if you reference the array, you will see the expected output:

var a = [9]; //suppose this is stores at 0x01

function changeto5(array){ 
  array[0] = 5
}

changeto5(a);//passes the address 0x01
console.log(a) //outputs [5]
Spectric
  • 30,714
  • 6
  • 20
  • 43
  • 1
    why would the array be changed in the function scope, if the address is passed in? surely, the value at the address would be updated. – Hasaan Khan Dec 16 '22 at 03:04
  • 1
    Because you’re modifying the **function’s** “array”, not the outer scope’s. Just because they’re called the same thing (and reference the same object) doesn’t mean they’re the same. – Dave Newton Dec 16 '22 at 03:08
  • 1
    Oh, that makes sense thank you. So the functions pointer is rehashed to a new address, which stores the new value, right? – Hasaan Khan Dec 16 '22 at 03:18