1

I'm having difficulties understanding the swapping of variables. There are many helpful threads explaining how to actually do it, but I am having difficulties understanding it. The example I'm talking about is:

var a = 1;
    b = 2;
    c = a;
a = b;
b = c;

In my (very basic) understanding I read that in plain english as: the variable c per declaration holds whatever the variable a is pointing at. Since we assign a = b after the declaration, shouldn't the next assignment make b hold the value 2 (because c is pointing at a which we just assigned to b)?

  • The variable `c` holds the same value that `a` is holding. These are not references. P.S. `b` and `c` are being declared *without* `var`. You want to have `var a = 1, b = 2, c = a;`. Notice the commas. – gen_Eric Jan 05 '15 at 17:06
  • Thanks, had i paid more attention I should have seen that with the declaration. I guess its a bit of a pitfall that JS accepts it nonetheless. –  Jan 06 '15 at 10:53

3 Answers3

7

JavaScript is call/assign by value (more specifically, call/assign by sharing) I.e. when you assign a variable to another variable, the value of the variable is copied. Assigning a new value to a variable never changes the value of another variable. There is no implicit link between them.

A bit more visual: Assuming that b holds the value v, then after a = b, we have

b -> v
a -> v

You seem to think that we have a -> b -> v instead, which is not the case.

In your example:

c = a; // c now holds the value 1
a = b; // a now holds the value 2
b = c; // b now holds the value 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 4
    If it is a simple type, not objects (and arrays). – Mörre Jan 05 '15 at 17:07
  • 2
    No, it applies to every type. In case of objects, the value if a reference. – Felix Kling Jan 05 '15 at 17:07
  • 2
    For complex objects it holds a reference, for simple ones it holds the actual value itself. I don't care how you call it (claiming the reference is a "value"), your explanation for a newbie is BAD if you do that. They will believe the object is the value and it is (deep) copied. – Mörre Jan 05 '15 at 17:08
  • @Mörre - javascript doesn't pass by reference, it's always pass by value, but for complex objects, the value holds a "copy of a reference". – adeneo Jan 05 '15 at 17:09
  • @Mörre: A reference to the *object*, not to the memory location of the *variable*. References are also copied. – Felix Kling Jan 05 '15 at 17:09
  • 1
    I have no idea what stuff you now come up with to still have yet another response??? And by the way, a reference is a pointer to a location in memory. – Mörre Jan 05 '15 at 17:10
  • 1
    @Mörre: I recommend to read http://en.wikipedia.org/wiki/Evaluation_strategy and learn about the difference between *pass by value* and *pass by reference*. – Felix Kling Jan 05 '15 at 17:11
  • Leaving out that little difference I mention is a BAD explanation esp. for newbies, end of story. I think you have no business telling me anything about what I should read, I seem to have a better grasp than you. – Mörre Jan 05 '15 at 17:11
  • @Mörre: What I explained in my answer holds true for every value type. – Felix Kling Jan 05 '15 at 17:12
  • 1
    @Mörre - But there is no passing references in javascript, saying there is would be even more wrong, it's all pass by value. – adeneo Jan 05 '15 at 17:13
  • Only that you failed to mention the difference between passing of simple values and complex ones. – Mörre Jan 05 '15 at 17:13
  • @adeneo If you declare a reference a value. But a reference is.. a reference! Hurra, what a surprise! – Mörre Jan 05 '15 at 17:13
  • @Mörre: And what's the difference? Are you telling me that in case of an object, e.g. `var a = {}; b = a; a = {foo: 42};`, `b` suddenly refers to `{foo: 42}` as well? – Felix Kling Jan 05 '15 at 17:14
  • No (filler for minimum character requirement). – Mörre Jan 05 '15 at 17:15
  • @Mörre: Now that I saw the edit to your second comment I see what you mean. However, I also think that explaining how objects work in the JS is out of the scope of this question/answer. – Felix Kling Jan 05 '15 at 17:25
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/68236/discussion-on-answer-by-felix-kling-understanding-basic-variable-concepts). – Taryn Jan 05 '15 at 17:48
  • Thanks for the explanation. So it was a misconception after all. –  Jan 06 '15 at 10:52
1

No, in JavaScript assigning the value of one variable to another assigns a copy of the value. Thus after

c = a;

the variable "c" has a copy of the value that's in "a". The subsequent assignment of a copy of the value of variable "b" to "a" has no effect on the value of "c".

Pointy
  • 405,095
  • 59
  • 585
  • 614
1

The assignment operator copies the value of the right hand side to the variable on the left hand side. It does not create a reference to a variable.

When the statement c = a runs, the value of a is still 1, so the value of c becomes 1.

You do not change the value of c anywhere else in the code, so when you reach b = c, the value is still 1.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335