92

I stumbled across some javascript syntax that seemed like it should produce a parse error of some kind but doesn't:

if (true, true) {console.log('splendid')} else {console.log('horrid')} // splendid
if (true, false) {console.log('splendid')} else {console.log('horrid')} // horrid

It seems only the last expression affects the logic, though all expressions are executed:

if  (console.log('super'), true) {console.log('splendid')} // super splendid

Anyone know why that is valid javascript syntax? Is there any practical use for it?

Matt
  • 2,140
  • 3
  • 16
  • 19

5 Answers5

87

The comma operator chains multiple expressions together, and the result of the operation is the value of the last operand. The only real use for it is when you need multiple side effects to occur, such as assignment or function calls.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 3
    Could you please be more specific about "assignment or function calls"? What do you mean? In general everything could be written without the comma operator as well. – Bergi Jul 18 '14 at 14:34
  • 19
    Some programmers wants to write a function by writing the fewest lines of code as possible. While this may be an interesting challenge, I think they often falls into unnecessary complexity making their code hard to understand / maintain. – The_Black_Smurf Apr 28 '15 at 19:36
  • 1
    doesn't make much sense to me that why is this allowed in the first place, If I were to rewrite the if statement written above in the question, I would simply put `console.log('super')` right above the if clause. – Saif May 06 '21 at 14:10
  • 2
    `if(a = getAValue(), b = getBValue(a), c = b * π, c >= 42) { /* whoa */ }` – seekerOfKnowledge Oct 17 '22 at 19:55
57

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Comma_Operator

https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Expressions_and_Operators#comma_operator

WillS
  • 362
  • 1
  • 12
Adam Ayres
  • 8,732
  • 1
  • 32
  • 25
  • 4
    Thanks for actually answering the question with sources.. pity I had to scroll through a bunch of other higher-rated answers to get to it – Mishax Jun 28 '15 at 12:40
  • The MDN documentation must have been updated as it now says "The comma operator evaluates each of its operands (from left to right) and returns the value of the LAST operand." – Yogi Mar 06 '16 at 12:06
20

commas in javascript are actually pretty arcane. The coolest use I have seen is this

while(doSomething(), checkIfSomethingHappened());

the most common would be the way var is used in modern js

var foo = 1,
    bar = 2;
Matt Briggs
  • 41,224
  • 16
  • 95
  • 126
  • 9
    That's what `do Something(); while (hasSomethingHappened())` loops are made for… – Bergi Jul 18 '14 at 14:32
  • 1
    I understand why it could be used in a loop statement. However, the OP question was about the `if` statements. – The_Black_Smurf Apr 28 '15 at 19:44
  • @Bergi ... do/while loops are just stupid, because you see statements before you see the condition. I actually quite like the comma operator used as in Matt's example. Ingenious. I bet I would get some raised eyebrows when I'll use it the next time round. – Robert Koritnik Aug 31 '17 at 11:02
  • *Brilliant example* in while statement. LOVE IT. It would likely be equivalent to `for(;doSomething(),checkIfSomethingHappened(););` – Robert Koritnik Aug 31 '17 at 11:05
  • 1
    @RobertKoritnik Well you see statements before the condition because they are executed before the condition? Also it's no different with the comma operator… – Bergi Aug 31 '17 at 11:11
7

This is also the same as in most other programming languages where you might have multiple iterators in a loop.

int x,y;
for(x = 0, y = 0; x < 10 || y < 100; x++, y++) {
....
}
Suroot
  • 4,315
  • 1
  • 22
  • 28
  • 5
    I understand why it could be used in a loop statement. However, the OP question was about the `if` statements. – The_Black_Smurf Apr 28 '15 at 19:44
  • ALthough in for statement it works differently, because first expression is only evaluated once while second and third are on each iteration. In if/while statement all expressions are evaluated on each visit of the statement. – Robert Koritnik Aug 31 '17 at 11:04
2

I permits to do operations and comparisons in the same context.

Example:

if(a = 2, a > 1) console.log('a is', a)
vinyll
  • 11,017
  • 2
  • 48
  • 37