w3resource logo


JavaScript Comma operator

JavaScript : Comma Operator

<<PreviousNext>>

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 a 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

<<PreviousNext>>

Rate this tutorial


Your Rating: not set

Share this tutorial

RSS Feed