Code 1 prints "florence"
let name = "florence";
function test (arr) {
arr += " and the machine";
}
test(name);
console.log(name);
Code 2 prints "florence and the machine"
let name = ["florence"];
function test (arr) {
arr[0] += " and the machine";
}
test(name);
console.log(name);
I understand that the outcomes are different because Code 1 passes by value, and Code 2 passes by reference. I'm just trying to understand why.
- What is the benefit of retaining the mutability of an array, but not a variable?
- CAN YOU pass variables by reference?
- CAN YOU pass arrays by value (without a workaround)?