https://www.codecademy.com/courses/introduction-to-javascript/lessons/arrays/exercises/array-in-functions
Hello everyone, I was checking the exercise in the link above and I was doing some tests.
I was just wondering regarding the concept of pass-by-reference, and I got a bit confused
So why would this code change the global and local variable
let array = ['why', 'you', 'no', 'change', '?'];
// attempting to mutate the array by removing the last index
function changeArr(arr) {
array.pop();
console.log("inside: ", arr);
};
changeArr(array);
console.log("outside: ", array);
//inside: [ 'why', 'you', 'no', 'change' ]
//outside: [ 'why', 'you', 'no', 'change' ]
while this code only changes the local variable
let variable = "Hello"
//attempting to change the variable using the pass-by-reference
function changeStr(text) {
text = 'Bye';
console.log('Inside: ' + text);
};
changeStr(variable);
console.log("outside: " + variable);
//Inside: Bye
//outside: Hello
appreciate your assistance on this