It is my understanding that non-primitive types in JS are passed by reference not by value.
However, in the following code, when I log test.array_reference I am getting the initial value of test_array rather than a reference to the updated array that I expect
function tester() {
let test_array = [];
function change_array() {
test_array = ["foo"];
}
return {
array_reference: test_array,
change_array: change_array
};
}
test = tester();
test.change_array();
console.log(test.array_reference); // why is this the initial value and not a reference to the updated array
What am I missing? Are arrays not always passed by reference?