1

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);
netuser
  • 553
  • 1
  • 6
  • 11
  • 1
    Possible duplicate of [Javascript by reference vs. by value](https://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value) – Spencer Wieczorek Jun 18 '17 at 03:12
  • 1
    @SpencerWieczorek. Is it because of this? `Changing the value of a variable never changes the underlying primitive or object. However, changing a property of an object referenced by a variable does change the underlying object.` - from the link you referenced in comment – netuser Jun 18 '17 at 03:23
  • Yes that's correct. – Spencer Wieczorek Jun 18 '17 at 03:46

2 Answers2

1

In the first example, when you set a = []; you are only changing the local variable a, which (after the assignment) no longer has any relation to the global variable that happens to have the same name. In the second example, you are directly modifying the stack variable, which happens to be the same object as the global variable a. If you were to do stack = whatever, it would behave similarly to the first example.

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • Thanks for answer. I didnt understand `If you were to do stack = whatever, it would behave similarly to the first example. ` part. If I set stack = [], it would be similar to first example? - Sorry, my bad. – netuser Jun 18 '17 at 03:27
  • Because that would change `stack` to be a reference to a new, different object. It would no longer refer to the same object as before. – Michael Geary Jun 18 '17 at 03:29
  • Another way to look at it: whenever you use `=`, you are changing the variable on the left to refer to a completely different thing than before. That's different from calling a method like `pop()`, which modifies the original object. – Michael Geary Jun 18 '17 at 03:32
  • I played around it as you explained. So if I change the property, the it affects the parent scope, but if I use assignment, it creates a new variable? Is my example correct in question? – netuser Jun 18 '17 at 03:35
  • Yes, it sounds like you have it right! Glad this was helpful. – Michael Geary Jun 18 '17 at 03:40
0

My guess is because in the first a is an object reference, which is set to a new empty array. Where as the 2nd calls a function on that object reference, which will actually remove elements from the array you pass to your function.

  • But as far as I know, all the operations are contained in the function scope, it should not affect the parent scope. Isn't it? – netuser Jun 18 '17 at 03:19