JavaScript only has pass-by-value. I just wanted to make it clear from the beginning.
To keep things simple, the code below illustrates the difference between pass-by-value and pass-by-reference.
Pass-by-value
function change (x) { x = 7; }
var x = 1;
change(x);
console.log(x); // 1
Pass-by-reference (not available in JavaScript)
function change (x) { x = 7; }
var x = 1;
change(x);
console.log(x); // 7
Now, you need to understand that some types are allocated on the heap, while others are allocated on the stack. So what does that mean?
Well, let's suppose you have a variable.
var x = 5;
Here the value lives on the stack. Which means, if I do
var y = x;
I will have two variables on the stack, both with their own copy of 5
. Simple types are always allocated on the stack.
Objects, on the other hand, live on the heap. When I do,
var o1 = {x: 5};
the object will be allocated on the heap, but the value within o1
variable, which is a reference to the object in the heap (let's call #ref1
the value within o1
variable), will be allocated on the stack. Which means, if I do
var o2 = o1;
I will have two variables on the stack, both with their own copy of #ref1
, but only one object allocated on the heap.
Now, you can use the reference to access object's members, and because both o1
and o2
contain a reference to the same object, they will be able to locate the object in the heap an change it's x
value.
If, however, I do the following
o2 = {x: 7};
this will change the value stored on the stack for the variable o2
(will call the new value #ref2
). Now, there are two objects on the heap and two references stored on the stack.
Finally, let's talk about your problem:
// The array (which is an object) is allocated on the heap
// {animals} variable will contain a reference to that object (#ref1)
var animals = ["dog", "cat"];
// Pass-by-value
pushSearchList(animals);
// Local variable {passedAnimals} will contain a copy of #ref1,
// but both {passedAnimals} and {animals} can modify the same object
function pushSearchList(passedAnimals){
var localAnimals = passedAnimals;
localAnimals.shift();
alert(animals);
}
Now, what if you want your own copy of the array object? Just clone the array using:
// {arr} will receive reference to a new array allocated on the heap (#ref2)
var arr = passedAnimals.slice(0);