We're looking for a person or people to help audit MDN to find places we could speed up. Is this you or someone you know? Check out the RFP: https://mzl.la/2IHcMiE

Naši dobrovolníci ještě tento článek do jazyka Čeština nepřeložili. Přidejte se a pomozte nám tuto práci dokončit!
Tento článek si můžete přečíst také v jazyce English (US).

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


expr1, expr2, expr3...


expr1, expr2, expr3...
Any expressions.


You can use the comma operator when you want to include multiple expressions in a location that requires a single expression. The most common usage of this operator is to supply multiple parameters in a for loop.


If a is a 2-dimensional array with 10 elements on each side, the following code uses the comma operator to increment two variables at once.

The following code prints the values of the diagonal elements in the array:

for (var i = 0, j = 9; i <= 9; i++, j--)
  console.log('a[' + i + '][' + j + '] = ' + a[i][j]);

Note that the comma in assignments such as the var statement may appear not to have the normal effect of comma operators because they don't exist within an expression. In the following example, a is set to the value of b = 3 (which is 3), but the c = 4 expression still evaluates and its result returned to console (i.e., 4). This is due to operator precedence and associativity.

// Note that the following creates globals and is disallowed in strict mode.

a = b = 3, c = 4; // Returns 4 in console
console.log(a); // 3 (left-most)

x = (y = 5, z = 6); // Returns 6 in console
console.log(x); // 6 (right-most)

The comma operator is fully different from the comma within arrays, objects, and function arguments and parameters.

Processing and then returning

Another example that one could make with comma operator is processing before returning. As stated, only the last element will be returned but all others are going to be evaluated as well. So, one could do:

function myFunc() {
  var x = 0;

  return (x += 1, x); // the same as return ++x;


Specification Status Comment
ECMAScript Latest Draft (ECMA-262)
The definition of 'Comma operator' in that specification.
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Comma operator' in that specification.
ECMAScript 5.1 (ECMA-262)
The definition of 'Comma operator' in that specification.
ECMAScript 1st Edition (ECMA-262)
The definition of 'Comma operator' in that specification.
Standard Initial definition

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support Yes Yes13 Yes Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes Yes4 Yes Yes ?

See also

Štítky a přispěvatelé do dokumentace

 Poslední aktualizace od: wbamberg,