|Written by Ian Elliot|
|Thursday, 24 October 2013|
No, of course not.
The Basic Comma
So to be 100% clear, the comma is only a comma operator when it acts on two expressions:
lefthand expression , righthand expression
Where expression is something that involves other operators, variables and functions.
What does the comma operator do?
It evaluates each of the expressions, starting with the lefthand expression and returns the value of the righthand expression.
stores 2 in a. Of course, the 1 and 2 are standing in for any complicated expression you care to think up, and you can use string expressions, functions and so on.
But why the parentheses?
which ends up storing 1 in a and throwing away the result of the righthand expression.
If you use more than one comma then, by the rules of operator precedence and left associativity, the result is that each expression is evaluated in turn from left to right and the last one is returned as the result. That is:
evaluates exp1, exp2 etc. and returns expN as its result.
For simple comma expressions like:
the value of the lefthand expression is always lost - so why bother?
The answer is that some operators have side effects and most functions have side effects.
For example, you can use:
and you will see 1,2,3 printed on the console and 4 stored in a.
Of course, given function calls and assignments are also statements we can use the semicolon:
The point is that semicolons separate statements and commas separate expressions, and in some cases statements are also expressions. There are places where you can only use commas, but often you have a choice.
The problem here is that some programmers choose to use a comma just because it looks cool and this is just confusing.
This Is Not The Comma You Are Looking For
One reason for confusion is that there are lots of places where commas are just separators.
For example, in object literals:
the commas are just separators.
the commas are just separators.
Perhaps most confusing of all is the var declaration. For example in:
the commas are just separators, not comma operators. The reason is that the var statement is a declaration and an initialization equivalent to
Things like a=1 is an expression; the value of the expression is, in this case one. This allows you to write things like:
which is equivalent to a=(b=1)
isn't an expression and doesn't have a value. Rather, it contains an expression because initialization is of the form:
This is subtle, but if you are following you can see that just by adding a pair of parentheses we can turn the previous var example into something that does use the comma operator:
What do you think is stored in a, b and c?
The answer is that a is 3, b is 2 and c is 3. The statement is of the form:
and the expression is:
which, by the rules of the comma operator, evaluates each sub-expression in turn, throws away each of the results except for the last, which is the value of the expression. Notice that the b=2 and c=3 are expressions, and if b and c don't exist they are created as global variables.
Now you should be able to see why:
is nonsense and just generates an error, but
is perfectly OK and declares two local varlables a and b.
What is stored in b?
Answer 3 because the expression is (2,c=3).
Using The Comma
So now we have the comma mastered - what can you use it for?
The honest answer is not much.
There are places, espcially in libraries, where you will find the comma operator in use, but in most cases there are much better and clearer ways of expressing the same idea.
The most commonly encountered use of the comma operator is to make more complex for loops. Really for loops should be kept simple so the idea of making them more complex isn't a good one.
Expression1 is evaluated once when the loop starts and it is usually where the initialization occurs. Expression 2 is evaluated before each new iteration of the loop. If it evaluates to false the loop stops. Expression 3 is evaluated at the end of each loop iteration and it is generally where loop counters etc are updated. Notice that you can write any expression you care to in a for loop and you can use the comma operator so that you can write multple expressions.
So, for example, what do you think the following for loop is all about?
The first expression isn't a use of the comma operator. It just uses the var statement to create two local variables, i and j, and sets them to zero and ten respectively. The second expression is a simple test for i being less than or equal to j, and doesn't use the comma operator. The final expression is the only use of the comma operator and it adds one to i and subtracts one from j each time through the loop. So the values of i and j we create are:
and then the loop ends.
You could just as easily have written the loop as:
You can use a similar technique to turn a while loop into a sort of for loop:
There are lots of similar uses and examples, but they all come down to the same thing - we need to use a single expression but also need to evaluate some other expressions first just for their side effects.
Relationship To Active Logic
The comma operator can be thought of as being a member of the same family as &&, || and ?
evaluates expressionA - if truthy then this is the result, otherwise the righthand expression is evaluated and is the result.
evaluates expressionA - if falsey then this is the result, otherwise the righthand expression is evaluated and is the result.
evaluates expressionA - if truthy it evaluates expressionB as the result and otherwise it evaluates expressionC as the result.
The comma operator:
evaluates expressionA then the righthand expression is evaluated and is the result.
It is also worth noting that the comma operator works well with any of the other active logic operators. In particular, the the ternary operator ? can often make use of it. For example:
If you assume that triesLeft is a counter of how many attempts the user has then you can decrement it and call the try function or call a function that informs the user that they have failed and then stops the program.
The situation is best summed up as "you need to understand the comma, but you probably don't need to use it".
What really matters is that you don't confuse the many other uses of the comma with the comma operator.
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
or email your comment to: email@example.com
|Last Updated ( Thursday, 24 October 2013 )|