19

The last few days, I've been helping a friend learn Javascript. It's his first language in years and he remembers virtually nothing, so he's been starting pretty much entirely from scratch. He's been going through a simple tutorial and I've been providing him with some exercises to help him practice. The most recent exercise I gave him was the (apparently) classic FizzBuzz problem. He solved it with a bit of help, but he did something very interesting while working out his solution. He came up with the following code:

for (var x = 1; x <= 100; x++) {
    if (x%3 == 0, x%5 != 0) {
        console.log("Fizz");
    }
    else if (x%3 != 0, x%5 == 0) {
        console.log("Buzz");
    }
    else if (x%3 == 0, x%5 == 0) {
        console.log("FizzBuzz");
    }
    else {
        console.log(x);
    }
}

He wasn't familiar with boolean comparison operators, so he didn't use && and instead used commas. My expectation was that it would crash and say something about a syntax error, but to my surprise it ended up running fine and just printing out a bunch of "Fizz" and "Buzz" lines, with no "FizzBuzz" or numbers. Needless to say, I was confused, so I did a bit of experimentation. My first test was to run this:

if (true, true) console.log('true, true');
if (true, false) console.log('true, false');
if (false, true) console.log('false, true');
if (false, false) console.log('false, false');

Which gave me two lines of output:

'true, true'
'false, true'

From that, I made the guess that all comma did was cause it to evaluate nothing but the last expression in the list. I then tried running this code:

for (var i = 0; i < 16; i++) {
    if ((Math.floor(i / 8) == 1), (Math.floor(i / 4) == 1), (Math.floor(i / 2) == 1), (i % 2 == 1)) {
        console.log(i);
    }
}

The output I got was all the odd numbers from 1-15, which confirmed my guess from my first test (since the last boolean in the comma-separated list was flipping every other iteration).

After all that long-winded context, my question is this: Is this comma syntax a known and intentional piece of the Javascript engine, or is it a strange, overlooked quirk of the interpreter? I know commas can be used for a few other things (initializing multiple variables in one line, declaring arrays, and separating parameters jump to mind), but I've never heard of them being used in conditional statements like this in any language, and I'm curious if anyone else knows whether or not this code should even run.

For reference, the FizzBuzz code and the second test I ran were both done using node, and the first test I ran was done in the Javascript console in Chrome, so it doesn't seem to be just a browser- or node-exclusive quirk if indeed it is one. Also, if you actually read this far, thank you.

ChrisProsser
  • 11,110
  • 6
  • 32
  • 42
Alex
  • 930
  • 6
  • 20
  • When all else fails, there is always the specification: [11.14 Comma Operator ( , )](http://www.ecma-international.org/ecma-262/5.1/#sec-11.14) – RobG Sep 05 '13 at 04:26
  • Now that you have some explanations of the comma operator, note that the code shown is syntactically valid but does not solve the FizzBuzz problem. (Or were you saying it was just a step on the way to a valid solution?) – nnnnnn Sep 05 '13 at 04:27

1 Answers1

27

The comma is an actual operator. It evaluates both of its operands (from left to right) and returns the value of the second operand.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator

Its most common usage is to supply multiple parameters in a for loop, but it can also be used for other purposes.

Robert Harvey
  • 168,684
  • 43
  • 314
  • 475
  • 2
    Huh. Apparently I'm bad at googling, then. I tried to look it up as an operator and failed entirely. Thanks for the quick reply. – Alex Sep 05 '13 at 03:57