Why do some operations not alter an array that was passed to a function?

Viewed 63

Scenario 1:

var myArray = [2, 3, 4, 5];
function doStuff(arr) {
  arr = [];
}
doStuff(myArray);
console.log(myArray); // [2,3,4,5]

Scenario 2:

var myArray = [2, 3, 4, 5];
function doStuff(arr) {
  arr.pop();
}
doStuff(myArray);
console.log(myArray); // [2,3,4]

Why does scenario 1 not update the globally declared array but scenario 2 does?

4 Answers
Related