Below is the example of the Comma operator.
-
Example:
<script>functionx() {document.write('one'+"</br>");return'one';}functiony() {document.write('two'+"</br>");return'two';}functionz() {document.write('three'+"</br>");return'three';}// Three expressions are// given at one placevarx = (x(), y(), z());document.write(x);</script>chevron_rightfilter_none -
Output:
one two three three
A comma operator (,) in JavaScript is used in the same way as it is used in many programming languages like C, C++, Java, etc. This operator mainly evaluates its operands from left to right sequentially and returns the value of the rightmost operand. A comma operator 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 on
In 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:
<script> function x() { document.write('Welcome'); return 'Welcome'; } function y() { document.write('to'); return 'to'; } function z() { document.write('Geeksforgeeks'); return 'Geeksforgeeks'; } // Three expressions are // given at one place var x = (x(), y(), z()); document.write(x); </script> |
Output:
In the output, first of all the function x() is executed then y() and lastly z(). Finally, comma operator returns rightmost expression.
The most useful application of comma operator is in loops. In loops, it is used to update multiple variables in the same expression.
Example:
<script> for (var a = 0, b =5; a <= 5; a++, b--) { document.write(a, b); } </script> |
Output:
Supported Browsers:
- Google Chrome
- Internet Explorer
- Firefox
- Apple Safari
- Opera
Recommended Posts:
- Create a comma separated list from an array in JavaScript
- Convert comma separated string to array using JavaScript
- Split a comma delimited string into an array in PHP
- How to create comma separated list from an array in PHP ?
- How to Convert CSV to JSON file having Comma Separated values in Node.js ?
- Convert ArrayList to Comma Separated String in Java
- Ternary operator vs Null coalescing operator in PHP
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript | ‘===’ vs ‘==’Comparison Operator
- JavaScript typeof operator
- Difference between == and === operator in JavaScript
- Arrow operator in ES6 of JavaScript
- JavaScript | Spread Operator
- What is the !! (not not) operator in JavaScript?
- JavaScript Instanceof Operator
- JavaScript Ternary Operator
- How to get negative result using modulo operator in JavaScript ?
- Difference between != and !== operator in JavaScript
- What is JavaScript >>> Operator and How to use it ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

