Stand in Line !Call by value or address?

Stand in Line !Call by value or address?
0

#1

function nextInLine(arr, item) {
arr.push(item); // Change this line
return arr.shift();
}
var testArr = [1,2,3,4,5];
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log(testArr);
console.log("After: " + JSON.stringify(testArr));
We are passing testArr to the function(seems as call by value to me) and using it in function by arr.
How come testArr gets changed?

According to me changes should not be reflected in testArr!


#2

If you give nextInLine an array and a value, it will mutate that array. So if testArr is passed to the nextInLine function, testArr will be modified.


#3

First off, it’s called call-by-reference, not address. (pass-by-reference is also correct)
Javascript does not have call-by-reference, only call-by-value.
Call-by-reference means the mutation to function argument persists after the function terminates.
In your example, the argument is an array, not an element of the array.
So, how do you mutate that argument? Here’s an example:

function test(arr) {
  arr=[];
  return;
}

myArr=[1,2,3,4];
test(myArr);
console.log(myArr);

If the console displays an empty array, then it is call-by-reference, otherwise by-value.

Some of the well-known languages that provide call-by-reference are C++, Php, C#.
In C++ and Php, you prefix the function parameter with &, in C# with keyword ref if you want call-by-reference.
Java and C are also on the same boat with Javascript, they’re call-by-value only.


#4

Then it indeed is, call by reference even though looks like we are dealing with a new array in function which is copy of array passed, but this would be like arr would have become an alias for the same array


#5

No, it is not.
You pass a reference, not the whole array.
The argument is that reference(known as pointer in C/C++), which is some 8-byte or 16-byte id, not all the array elements.


#6

See what i found on w3schools
Objects are Passed by Reference
In JavaScript, object references are values.

Because of this, objects will behave like they are passed by reference:

If a function changes an object property, it changes the original value.

Changes to object properties are visible (reflected) outside the function.


#7

The example in my first reply answers everything. Unless you’ve run it, it does not display an empty array, the array remains the same, so the mutation did not persist, hence by-value.

The things you manipulate via the pointer(reference) are different from that pointer.
What you pass to the function is that pointer. That is the argument.

The term reference used in reference to object is different from the reference used in pass-by-reference and we owe that confusion to Java, unfortunately (Java designers particularly avoided using the term “pointer”, and came up with reference instead).


#8

Yes, it is not changing to an empty array but how come in my example change persist if at all javascript doesn’t offer call-by-reference?


#9

I also described that.
It’s normal for you to get confused because that pointer thing was meant to be totally opaque to Javascript developers, but it unfortunately surfaces in this case.
There’s an id, fixed in size like 8-byte or 16-byte, and there are data addressed by this id.
The arr holds that id. When you say arr[0] you dereference the data addressed by that id.
Now, as a Javascript programmer you know nothing about that id, all you care is the data. The array is the data, right?
Well, from the point of Js engine, the id and data addressed by it are different things, and when it comes to passing as argument it only passes that id. The argument is that id. The changes you make on that id defines call-by value or by-reference, not what you do as arr[0] or arr.push()
In my example I changed that id by directly assigning a new aray to arr.
The same is also true for objects, like obj.prop. Here obj by itself holds the id, and the dot operator dereferences to data addressed by it.


#10

well this sounds great! Thanks :wink:


#11

Here’s another example, this time I passed an array item, not the array(id):

function test(arr) {
  arr=9;
  return;
}

var myArr=[1,2,3,4];
test(myArr[0]);
console.log(myArr);

The first element of the array is still 1, not 9, the change did not persist after function call, hence by-value.