JavaScript Comma Operator
JavaScript Comma Operator mainly evaluates its operands from left to right sequentially and returns the value of the rightmost operand. It is used as a separator for multiple expressions at a place that requires a single expression. When a comma operator is placed in an expression, it executes each expression and returns the rightmost expression.
Syntax
Expression1, Expression2, Expression3, ...so onIn the above syntax, multiple expressions are separated using a comma operator. During execution, each expression will be executed from left to right and the rightmost expression will be returned.
Example 1: Below is an example of the Comma operator.
function Func1() {
console.log('one');
return 'one';
}
function Func2() {
console.log('two');
return 'two';
}
function Func3() {
console.log('three');
return 'three';
}
// Three expressions are
// given at one place
let x = (Func1(), Func2(), Func3());
console.log(x);
Output
one two three three
Example 2: The most useful application of the comma operator is in loops. In loops, it is used to update multiple variables in the same expression.
for (let a = 0, b =5; a <= 5; a++, b--) {
console.log(a, b);
}
Output
0 5 1 4 2 3 3 2 4 1 5 0
Example 3: Using the comma operator to initialize multiple variables in a single statement.
let a = 1, b = 2, c = 3;
console.log("Initial values:");
console.log("a:", a);
console.log("b:", b);
console.log("c:", c);
// Using comma operator to update multiple variables
(a *= 2), (b *= 3), (c *= 4);
console.log("Updated values:");
console.log("a:", a);
console.log("b:", b);
console.log("c:", c);
Output
Initial values: a: 1 b: 2 c: 3 Updated values: a: 2 b: 6 c: 12
We have a complete list of JavaScript Operators, to check those please go through the Javascript Operators Complete Reference article.
Supported Browsers
- Google Chrome
- Firefox
- Apple Safari
- Opera
JavaScript Comma Operator – FAQs
What is the comma operator in JavaScript?
The comma operator allows multiple expressions to be evaluated in a single statement, returning the value of the last expression.
How does the comma operator work?
When the comma operator is used, each expression is evaluated from left to right, but only the result of the final expression is returned.
Where is the comma operator commonly used?
The comma operator is often used in for loops to include multiple expressions within the loop initialization or increment sections. It can also be used in variable assignments and other contexts where multiple operations need to be performed in sequence.
Can the comma operator be used in variable declarations?
Yes, the comma operator can be used to include multiple expressions in a single variable declaration statement, but only the last expression’s value will be assigned to the variable.
How does the comma operator compare to semicolons in JavaScript?
Semicolons separate statements, each of which is evaluated independently. The comma operator allows multiple expressions to be evaluated within a single statement, with only the last expression’s result being returned.
Can the comma operator be used in function arguments?
Yes, the comma operator can be used in function arguments to evaluate multiple expressions, but only the last expression’s value will be passed as the argument.

