JavaScript passes and returns by value, but the array []
is an object, and returning the result or assigning it to a variable doesn't make a copy of that object. All three names—table
, res
, and result
—all refer to that same array object, so when the code in setTimeout
changes the array, then you see it in result
later on.
// primitives like numbers and strings pass by value
let string1 = "abc";
let string2 = string1;
string2 = "def";
console.log(string1 + " / " + string2); // abc / def
let number1 = 123;
let number2 = number1;
number2 = 456;
console.log(number1 + " / " + number2); // 123 / 456
// if you replace the reference to the array, then they coexist
let array1 = [234];
let array2 = array1;
array2 = [567];
console.log(array1 + " / " + array2); // 234 / 567
// but if you modify it in place, then there's only one object
// so the modification shows in both places
let array3 = [345];
let array4 = array3;
array4.push(678);
console.log(array3 + " / " + array4); // 345,678 / 345,678
Your function test
returns an array object, and that object is the object passed to then
and saved in result
. However, the browser (or other environment) still runs the setTimeout
code, changing the contents of the array even after the reference to the array was saved to res
and result
.
For this reason, you have to be very careful when working with an array you've received as a variable: If you need to ensure it doesn't change unexpectedly, you might need to make your own copy of the array. Otherwise, the caller might change the array out from under you, exactly the way that your setTimeout
does in test
.