Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140611

When are arrays not passed by reference [duplicate]

$
0
0

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?


Viewing all articles
Browse latest Browse all 140611

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>