8

Just bumped into the fact that an if statement can have multiple parameters in javascript:

// Webkit
if (true, true, false) console.log("this won't get logged");

How well is this supported?

p.s. I get that this is similar to using &&, but this is interesting and a google couldn't provide the answer.

Ross
  • 14,266
  • 12
  • 60
  • 91
  • 2
    Using this syntax in production code is one of the 7 deadly programming sins. – riwalk Aug 25 '11 at 20:40
  • Can you please provide the source for where you found that? Not that I don't believe you, but this looks interesting. – HyderA Aug 25 '11 at 20:41
  • Possible duplicate: http://stackoverflow.com/questions/703896/as3-javascript-if-statement-commas-instead-of – Joe Aug 25 '11 at 20:42
  • essentially this is only useful in code-golfed JavaScript – zzzzBov Sep 01 '11 at 17:22

4 Answers4

14

If statements can't have "multiple parameters". What you observed is the use of the comma operator.

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

Now we see why (true, true, false) doesn't log anything whereas (true, false, true) will. As a side note, this syntax is ubiquitously supported (but should almost never be used).

David Titarenco
  • 32,662
  • 13
  • 66
  • 111
  • Wait! So why does it need to evaluate when it's simply returning the second operand? – HyderA Aug 25 '11 at 20:42
  • 3
    @gAMBOOKa: In the example, not at all. When the left expression has side effects, precisely for those. –  Aug 25 '11 at 20:43
  • I am afraid I don't fully understand it. What do you mean 'has side effects'? – HyderA Aug 25 '11 at 20:45
  • 2
    @gAMBOOKa, `destroy_the_world(), true` will evaluate to `true` but also destroy the world. – Matthew Flaschen Aug 25 '11 at 20:46
  • yip. tick tock, before the world ends and when SO lets me answer, I will mark this correct. – Ross Aug 25 '11 at 20:48
  • Ah! My interpretation of 'evaluate' was 'compare'... stupid me! – HyderA Aug 25 '11 at 20:49
  • 5
    `i = 10, i === 10` evaluates to true, since the left side has the side-effect of assigning the number 10 to i. `i === 0, i = 10` evaluates to 10, and is the same as just writing `i = 10`, since the left side has no side-effect. – Paul Aug 25 '11 at 20:50
  • I wouldn't say "almost never." It's convenient in some cases, like multiple initializations in a for loop. – Matthew Flaschen Aug 25 '11 at 20:54
  • @Matthew every style guide I've read for the several companies I've worked for has denounced using the comma operator. I think it's rather widely regarded as difficult/cryptic to understand. – David Titarenco Aug 25 '11 at 20:57
  • @David, I've read several places ([this](http://stackoverflow.com/questions/703896/as3-javascript-if-statement-commas-instead-of/703915#703915) being just one) that it is idiomatic to use it in a for loop initialization as I described. – Matthew Flaschen Aug 25 '11 at 21:01
6

That is not a feature of the if statement, it's the , operator.

You can separate expressions with a comma, and each expression will be evaluated but only the last one is used as value of the whole expressions.

In your example it's pretty useless, but sometimes you want an expression evaluated for it's side effects. Example:

var i = 1;
if (i += 2, i == 3) console.log("this will get logged");
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Oooooh, I wish I could upvote again after that edit haha, nice example – Paul Aug 25 '11 at 20:51
  • Why can you not use `var` inside the `if()` as you can with `for()`? (I know this doesn't relate to your specific example, but just wondering?) – MrWhite Aug 25 '11 at 22:48
  • 1
    @w3d: That is a special feature in the first expression in the `for` statement, which is there because you often need a variable for the loop. – Guffa Aug 26 '11 at 07:23
3

It's not similar to && (try moving the false to the first or second place), it has nothing to do with if, and it's about as well supported as function literals, i.e. absolutely universally. What you encountered is the comma operator inherited from C, that evaluated the expressions (actually, it's always two expressions and using more conceptually leads to nesting like a + b + c does) from left to right and throws away all except the last result. The last result becomes the result of the whole expression.

1

Works in IE7 so I guess this is well supported.

Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • 2
    Relying on IE to evaluate standards? – HyderA Aug 25 '11 at 20:43
  • 4
    Nope - its the LCD (lowest common denominator) test. If any feature is likely to break, its probably IE 6 or 7! :D – Mrchief Aug 25 '11 at 20:45
  • 1
    About 50% of the time if something does worksin IE it's not going to work anywhere else haha :P, they like to do things their own way. Obviously the comma operator works in all browsers. It was part of Javascript 1.0, and if I'm not mistaken as always been part of ECMAScript. – Paul Aug 25 '11 at 20:47
  • @Paul: Yes (but that's true _mostly_ in case of CSS/HTML). Also in this case WebKit was verified already. And I've stopped using FF... ;) – Mrchief Aug 25 '11 at 20:50
  • @Mrchief I'm afraid the LCD argument doesn't really work with IE. It's often the case that if something works in IE 7, it's by accident rather than by design. – Jared Ng Aug 25 '11 at 21:01
  • @Jared: First, you guys are taking this way too seriously :). Secondly, either way, you care about if it works or not, right? Lastly, webkit was verified in this case. Also, now-a-days, people design for FF/Webkit and then come to IE to see what breaks, so in that regard, I said that. – Mrchief Aug 25 '11 at 21:03