w3resource

JavaScript: Comma Operator

Description

The comma operator (,) is used to execute two expressions sequentially.

Syntax

expr1, expr2

Parameters

expr1: Any expressions.

expr2: Any expressions.

We use the comma operator when we want to include multiple expressions in a location that requires a single expression. This operator is generally used inside a for loop, to allow multiple variables to be updated (increase/decrease) each time through the loop.

For example, we want to display a serial No. (starting from 1) and an employee code ( starting from 10 ), the following code uses the comma operator to increment two variables at once.

for (var i=1, j=10; i <=10; i++, j++)
{
alert('Sl. No. : '+i+' Employee Code : '+j)
} 

See also

Conditional Operator
delete
function
in
instanceof
new
this
typeof
void

Previous: JavaScript: Conditional Operator
Next: JavaScript: delete Operator

Test your Programming skills with w3resource's quiz.



JavaScript: Tips of the Day

Creates an object from the given key-value pairs

Example:

const tips_objectFromPairs = arr => arr.reduce((a, [key, val]) => ((a[key] = val), a), {});
console.log(tips_objectFromPairs([['x', 2], ['y', 4]])); 

Output:

[object Object] {
  x: 2,
  y: 4
}