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.



Share this Tutorial / Exercise on : Facebook and Twitter

JavaScript: Tips of the Day

Using the window.location object

JavaScript can access the current URL using the window.location object. Pretty neat, but even cooler is that this object contains certain parts of the URL as well.
Get access to the protocol/host/pathname/search/and more!

// JavaScript can access the current URL in parts. For this URL:
'https://thatsanegg.com/example/index.html?s=article'
window.location.protocol == 'https:'
window.location.host == 'thatsanegg.com'
window.location.pathname == '/example/index.html'
window.location.search == '?s=article'

Ref: https://bit.ly/2LBj213