In the following JS code, why doesn't f3(arr2)
change the value of arr2
like f2(arr1)
did to arr1
? Is there any way to make f3
work as expected (if possible, without returning the modified array)?
var arr1 = [1, 2, 3, 4];
var arr2 = [1, 2, 3, 4];
function f1() {
return [2, 3, 4, 5];
}
function f2(arr) {
arr.push(5);
}
function f3(arr) {
arr = f1();
}
f2(arr1);
console.log(arr1); // [ 1, 2, 3, 4, 5 ]
f3(arr2);
console.log(arr2); // [ 1, 2, 3, 4 ], expect [2, 3, 4, 5]