0

I have seen in some nodejs scripts variables/objects being used like this:

var myvar1 = myvar2 = myvar3;

Why is this used and what does it mean?

c69
  • 19,951
  • 7
  • 52
  • 82
wilsonpage
  • 17,341
  • 23
  • 103
  • 147
  • 2
    Lots of enthusiastic answers, but only a couple mentioning the scope correctly. – Chris Morgan Sep 22 '11 at 14:28
  • possible duplicate of [Multiple left-hand assignment with JavaScript](http://stackoverflow.com/questions/1758576/multiple-left-hand-assignment-with-javascript) – Chris Morgan Sep 22 '11 at 14:33

6 Answers6

4

It will evaluate to:

var myvar1 = myvar2 = myvar3;
var myvar1 = myvar3;          // 'myvar2' has been set to 'myvar3' by now
myvar3;                       // 'myvar1' has been set to 'myvar3' by now

It will first assign myvar3 to myvar2 (without var, so possibly implicit global, watch out for that).

Then it will assign myvar3's value to myvar1, because the set value is returned.

The result is again returned, which won't do anything further - in the final line nothing happens with myvar3's value.

So in the end they have all the same value.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 1
    Don't you mean something more like `myvar2 = myvar3`, `var myvar1 = myvar2`? Anyway, the fact that `myvar2` isn't inserted into the local scope is important. +1. – Chris Morgan Sep 22 '11 at 14:26
  • @Chris Morgan: I'm not sure whether the left hand side or the right hand side is returned. However, since both will have the same value, I think there is no real difference in that. – pimvdb Sep 22 '11 at 14:29
  • it's just that what you've got there doesn't make much sense. Reread it... you're answering the question with the question. – Chris Morgan Sep 22 '11 at 14:31
3

This sets myvar2 to myvar3, and sets myvar1 to myvar2.

I assume myvar3 and myvar2 have been declared before this line. If not, myvar2 would be a global (as there's no var), and if myvar3 wasn't defined, this would give an error.

This expands to:

myvar2 = myvar3; // Notice there's no "var" here
var myvar1 = myvar2;
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 1
    I think `myvar2` is more likely to not have been declared than declared; the author of the code probably doesn't realise the scope implications of it. – Chris Morgan Sep 22 '11 at 14:29
3

If:

var myvar3 = 5;

var myvar1 = myvar2 = myvar3;

Then they are all = 5

Naftali
  • 144,921
  • 39
  • 244
  • 303
3

myvar1 and myvar2 both get the name of myvar3. the first expression to evaluate is myvar2 = myvar3. This assigns myvar3 to myvar2. The result of this operation is the assigned value , and this is then assigned to myvar1.

Dennis
  • 14,210
  • 2
  • 34
  • 54
1

This will assign the variables myvar1 and myvar2 to the value of myvar3. Why they do this is unknown to me, but my best guess is they want the two vars to be the same value of myvar3.

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
  • 1
    Probable use: initialising a couple of variables to a certain value. `current = minimum = foo.minimum`, for example. – Chris Morgan Sep 22 '11 at 14:30
1

as already explained, the statement results in all variables having the value myvar3.

I like to add: using statements like that you have to beware of scope, demonstrated by:

function foo(){
  var c = 1;
  var a = b = c;
  console.log(a,b,c); //=> 1 1 1
  c = 2;
  console.log(a,b,c); //=> 1 1 2
}
console.log(b); //=> 1! [b] is now a variable in the global scope 

And of assigning non primitive values (so, references to objects)

function foo(){
  var c = {};
  var a = b = c;
  c.bar = 2;
  console.log(a.bar,b.bar,c.bar); 
       //=> 1 1 1 (a, b and c point to the same object)
}
KooiInc
  • 119,216
  • 31
  • 141
  • 177