25

Possible Duplicate:
Javascript syntax: what comma means?

I came across the code while reading this article (do a Ctrl+F search for Andre Breton):

//function returning array of `umbrella` fibonacci numbers
function Colette(umbrella) {
  var staircase = 0, galleons = 0, brigantines = 1, armada = [galleons, brigantines], bassoon;
  Array.prototype.embrace = [].push;

  while(2 + staircase++ < umbrella) {
    bassoon = galleons + brigantines;
    armada.embrace(brigantines = (galleons = brigantines, bassoon));
  }

  return armada;
}

What does the x = (y = x, z) construct mean? Or more specifically, what does the y = x, z mean? I'm calling it comma assignment because it looks like assignment and has a comma.

In Python, it meant tuple unpacking (or packing in this case). Is it the same case here?

Community
  • 1
  • 1
Yatharth Agarwal
  • 4,385
  • 2
  • 24
  • 53

3 Answers3

24

This is the comma operator.

The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.

The resultant value when a,b,c,...,n is evaluated will always be the value of the rightmost expression, however all expressions in the chain are still evaluated (from left to right).


So in your case, the assignations would still be evaluated, but the final value would be bassoon.

Result:

galleons = brigantines
brigantines = bassoon
armada.embrace(basson)

More information: Javascript "tuple" notation: what is its point?

Community
  • 1
  • 1
mbinette
  • 5,094
  • 3
  • 24
  • 32
10

var syntax allows multiple assignment, so when you see the following, you're declaring multiple variables using one var statement.

var a, b, c;

Note that this syntax is not the comma operator.


The , can be used as the comma operator. It simply evaluates a series of expressions. So when you see the following syntax, you are seeing a series of expressions being evaluated, and the return value of the last one being returned.

x = (y = x, z)

Within the parens, x is assigned to y, then z is evaluated and returned from the () and assigned to x.


I'd suggest that this syntax is unclear and offers little benefit.

I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77
  • 2
    Neutral at best and actively harmful on average. Typical JavaScript :/ – Yatharth Agarwal Oct 07 '12 at 17:11
  • 6
    A programming language can only be as good as the programmers who use it! ;-) You can do some pretty nasty things with the JVM (in Java) to mess with other programmers (change a class definition during execution? check.). Same for pointers in C/C++ (I'm talking about abusive use of `*(*(*(*(*(*p)))))`, not legit pointer use). In PHP? Don't even get me started..! But I would definitely ban this comma use from good practices. I must admit I cannot even think of a legit use for it (other than declaring variables, and enumerations, of course..). – mbinette Oct 07 '12 at 18:09
9

The comma operand evaluates all of its operands and returns the last one. It makes no difference in this case if we had used

x = (y = x, z);

or

y = x;

x = z;

It's there to take away that line of code.

David G
  • 94,763
  • 41
  • 167
  • 253
  • @YatharthROCK There is no point but put the code on one line. It's as simple as that. – David G Oct 07 '12 at 17:14
  • Why would you want to do that? It actually uses more characters (9 as opposed to 8). Then what possible justification could there be? – Yatharth Agarwal Oct 07 '12 at 17:15
  • @YatharthROCK It *would* be better to use single statements instead of the comma operator, but sometimes it's just the preference of the writer. If the variable names are short and the goal of `x = (y = x, z);` is easy to understand, then I'll assume it's okay to write it that way. – David G Oct 07 '12 at 17:19
  • No, it's not. I don't see good reason why this couldn't be written as `y = x; x = z` and the bad effects, you already know. – Yatharth Agarwal Oct 07 '12 at 17:47
  • 5
    Yatharth: There is really no point in that ugly code. Quite frankly, I have no idea why anyone would use it, other than mess up with future programmers (or maybe that was the whole point, make sure he remains the only programmer on the project?). It's like those people in PHP who go like: `$hi = 'oo!'; $myVar = 'hi'; $a = 'myVar'; echo $$$a; //prints 'oo!'.` - Some men just want to watch the world burn. I would've wrote it like I did in my answer (the **result** part). – mbinette Oct 07 '12 at 17:58
  • @mbinette please read this. http://js1k.com/2014-dragons/rules maybe for coding competitions. – loki9 Nov 06 '14 at 03:40
  • @YatharthAgarwal if the purpose is to return modified objects [(example)](https://stackoverflow.com/a/49406071/6225838) while possibly combining with ES6 features, this comma syntax can be useful. – CPHPython Mar 21 '18 at 12:28