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!
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.
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
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.
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).
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.