Comma operator (,) (Delimiter)

An argument separator token.

Availability:

ECMAScript edition - 2
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Netscape Enterprise Server - 2.0
Opera - 3.0

The comma operator provides a way to evaluate several assignment expressions at once.

It is provided as a way to express the arguments to a function in a manner consistent with the ECMA standard.

It is also used in variable declaration lists to declare more than one variable with a single var statement.

Its most subtle use is to exploit the side effects of an expression without assigning the result of that expression to anything. This is actually so subtle as to confuse what it's actually doing. There is probably a simpler way to express the same functionality that probably doesn't execute any slower and is a lot easier to maintain.

The associativity is left to right. This means that the expressions should be executed in left to right order of appearance in the script source text. The result of the entire expression will be the evaluation of the last comma separated item. It is not good style to use a comma separated list as an RValue in an assignment to an LValue.

Example code:

   // Declaring several variables
   var e, c, d, x, y;
   
   // Arguments in a for iterator header
   for(a=0; a<10; a++, b++)
   {
   }
   
   // Add c to d and assign the result to e with incrementing
   // side effects
   e = (x++, y++, c) + (z--, d)

See also:Associativity, Comma expression, Document.write(), Document.writeln(), Operator Precedence, var

Cross-references:

ECMA 262 edition 2 - section - 11.14

ECMA 262 edition 3 - section - 11.14

Wrox Instant JavaScript - page - 21