Comma Operator (,) Primarily evaluates its operands from left to right sequentially and returns the value of the rightmost operand. The comma operator is used as a delimiter for multiple expressions at a location where only one expression is required. When placing the comma operator 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 its execution, each expression will be executed from left to the right and the rightmost expression will be returned.

Example 1: Below is an example of the Comma operator.

JavaScript

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.

JavaScript

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

We have a complete series of JavaScript topics, please go through the JavaScript articles here.

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Apple Safari
  • Opera
Whether you are preparing for your first job interview or planning to improve your skills in this ever-evolving tech landscape, CodeConfig.in Courses will help you in achieving success. We provide top-quality content on JavaScript at no cost, all geared towards accelerating your growth in the limited time period. Join the millions we have already helped, and we are here to do the same for you. Don’t miss out – check it out now!
Please Check all JavaScript articles HERE. For more such post you can refer HERE.

Explore More

How to Center a Popup Window on Screen

JavaScript window.open() method is used to open a popup window. This popup window will be placed in the center of the screen. This example creates the pop-up window without placing it into

How to hide URL in the popup window opened using window.open

If you are using below code to open a popup window in your web page, then address must be appearing in you pop up window and f you want to

window.showModalDialog is deprecated in Edge and Chrome

If you have a website being compatible with Edge/Chrome and in past it was only compatible with IE 11 and if you are using window.showModalDailog in your JavaScript code, then