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);

3
  • 1
    Possible duplicate of Javascript by reference vs. by value Commented Jun 18, 2017 at 3: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 Commented Jun 18, 2017 at 3:23
  • Yes that's correct. Commented Jun 18, 2017 at 3:46

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
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.
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?
Yes, it sounds like you have it right! Glad this was helpful.
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.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.