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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript/operators/comma.php