Scenario 1
var a = [1, 2, 3];
function isChanged(a) {
a = [];
}
isChanged(a);
console.log(a);
Scenario 2
var a = [1, 2, 3];
function isChanged(stack) {
while (stack.length != 0) {
stack.pop();
}
}
isChanged(a);
console.log(a);
Why is the array not empty in the first function, but it is empty when I use the second one?
Edit :
I played around the changing the variable by assignment, and overriding its property.
Scenario 3 - changing the property of object
var a = {
prop: "Stackoverflow"
}
function change(a) {
a.prop = "stack"
}
change(a)
console.log(a)
Scenario 4 - changing the whole variable itself
var a = {
prop: "Stackoverflow"
}
function change(a) {
a = {
"prop": "stack"
}
}
change(a);
console.log(a);