1

I was curious, so I tried this out:

var test = example = 5;

Now, this is a somewhat common way of defining things...

(for instance in node, module.exports = exports = ...)

The above be equivalent to:

example = 5;
var test = example;

Now, if variables don't actually hold a value, but are rather references to values in the memory, when I change example, if test is referencing example which is referencing a value, shouldn't test also have that same value as example? Lets try it:

var test = example = 5;
example = 7;

console.log(test, example);

... Now, the output isn't really what I expected, due to what I put above.. You get this:

5 7

But how is test still 5 if test is referencing example, and example is now 7? Shouldn't they both be 7?


EDIT Tried using Objects instead... But I get the same behavior:

var test = example = {"hello":"world", "foo":"bar"};
example = {"test":"example"};
console.log(test, example);

This outputs:

Object {hello: "world", foo: "bar"} 
Object {test: "example"}

So, they're still not the same like I expected.

  • 1
    Numbers are immutable and everything is passed by value. – elclanrs Jul 21 '15 at 10:05
  • If you used an object instead of 5, then you would achieve the result you are expecting, because js objects are mutable – Ananth Jul 21 '15 at 10:07
  • @elclanrs it's actually not about immutability (at all), but about "calling by sharing" (it would be exactly the same behaviour even if you used a mutable `{}` instead of `5`) – zerkms Jul 21 '15 at 10:09
  • @Ananth no, they would not (and again - it has nothing to do with immutability) – zerkms Jul 21 '15 at 10:11
  • 2
    Actually `var test = example = 5;` is equivalent to `example = 5; var test = 5;`. Assignment returns right value by spec – Eugene Jul 21 '15 at 10:13
  • @Zhegan, alright thanks. But even so I still get the same effect. –  Jul 21 '15 at 10:15
  • @Jamen why should it be different? References != pointers. With pointers you would get such a behaviour. – zerkms Jul 21 '15 at 10:15
  • > var test = example = {} > test.blabla = 'b' > example Output: Object {blabla: "b"} – Ananth Jul 21 '15 at 10:16
  • Seems like it should reference the same value in the memory, maybe I just don't completely understand. –  Jul 21 '15 at 10:17
  • @Jamen they do reference to the same value in memory. Then you create another object and put a reference to it to just one variable. The other variable still holds a reference to the old one. – zerkms Jul 21 '15 at 10:17
  • Ohh, I see. That's pretty much the answer to this post then. –  Jul 21 '15 at 10:18

2 Answers2

0
var a = {}; // There is one object in memory
var b = a;  // now both `b` and `a` refer to that object

a = {};     // now we create another object and put a reference to it to an `a`
            // we haven't changed `b` so it refers to the former object

And to summarize: in JS you cannot change the value of another variable. Value of variables a and b in the example above is a reference, not the object itself. And the only way to change the value of the variable - is to re-assign another value to it. So you cannot change the value of a variable indirectly in JS, at all (?).

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • So, just curious, if you remove all references to that first object, would the object be destroyed? Or (for instance if we were in a web browser) would we have to wait until the page reloads? –  Jul 21 '15 at 10:20
  • @Jamen if an object in the heap is not reachable (there are no any references to it) then a JS engine is free to garbage collect it. But it's not obliged to do so, since the standard does not qualify how engines should perform garbage collection. And to be 100% precise - it does not require an engine to have a garbage collector at all. – zerkms Jul 21 '15 at 10:22
  • If two variables reference the same object, and you add a property to one of the variables, couldn't it be argued that you've indirectly "changed" the other variable? – Rick Hitchcock Jul 21 '15 at 10:32
  • @RickHitchcock you don't change a variable. You change the object in the heap. The values of the variables are still the same reference. It's like - you have a postal address like `42, Foo bar avenue`. I have the same postal address. And a guy with name "A A" lives there. Then he moves somewhere else and now a lady "B B" lives there. We have the same reference (address), it has not changed. But the contents (the tenant) has changed. `zerkms.friend_address = rick.friend_address = { tenant: 'A A' }; zerkms.friend_address.tenant = 'B B';` – zerkms Jul 21 '15 at 20:20
  • Right, I understand how that works. But if a variable's property has changed, some might argue that the variable's "value" has changed. Example at http://jsfiddle.net/744tawy1/1/ – Rick Hitchcock Jul 21 '15 at 21:35
  • 1
    @RickHitchcock "some might argue" --- and it's fine :-) This is a tricky terminology question so no wondering some people have hard time getting into it. The main part is that the object is not a value or a variable, but a reference is. "if a variable's property has changed" --- and it's critically important to not say "a variable's property", since a variable does not have one, it's an object property has changed, which a variable refers to. – zerkms Jul 21 '15 at 22:00
0

When speaking about java script, variables can accept multiply values. JavaScript is a very 'forgiving' language let's call it like this.

So, if you said for example:

 var test = example = 5;

test would still be 5, because test is now given the value of 5. test is but a reference in this case to example.

the actual value of example, is 7.

example = 7;

So yes, it's a bit odd, But javascript behaves a bit differently then other programming languages.

Barr J
  • 10,636
  • 1
  • 28
  • 46
  • "But javascript behaves a bit differently then other programming languages." --- like what "other" languages for example? I see it behaves the same way like majority of languages I ever worked with. – zerkms Jul 21 '15 at 10:23
  • `test` is not a reference to `example`. Instead, it's the result of the expression `(example=5)`, which is simply `5`. – Rick Hitchcock Jul 21 '15 at 10:24