Q:

function passing by reference

after I run below codes why myArray is still [2, 3, 4, 5], why it is not changed to []? 
var myArray = [2, 3, 4, 5];
function doStuff(arr) {
  arr = [];
}

doStuff(myArray);

However when I do below steps , myArray did change to [2, 3, 4, 5, 6]
var myArray = [2, 3, 4, 5];
function doStuff(arr) {
  arr.push(6);
}

doStuff(myArray);

I am very confusing about this.

A:

arr is a local variable in
function doStuff(arr) {
  arr = [];
}

The line arr = []; assigns a brand new object to arr, so that it no longer points to the passed array. Since the local variable arr goes out of scope when the function returns, this assignment has no effect. It certainly doesn't effect the non-local variable myArray in the caller's scope.
On the other hand, the second function:
function doStuff(arr) {
  arr.push(6);
}

actually does something with the passed array, calling the push method on the object that arr names. Note that arr isn't redefined in the body of the function, so that it never loses its identity as a name for the passed array.

