6 ms·
The JavaScript Comma Operator
- gerggerg 15y agoReduces readability without reducing file-size. This belongs more on http://wtfjs.com/ http://wtfjs.com/ .
- vosper 15y agoAbsolutely - I'd ask this to any interviewees claiming to be javascript experts but I'd never want to see it in production.
- ketralnis 15y ago> I'd ask this to any interviewees claiming to be javascript experts but I'd never want to see it in production That's a curious way to find programmers that are going to write code that you plan to use in production
- zephjc 15y agoThe comma in its typical uses in variable assignment is pretty useless IMO. var x = 1, y = 2; Destructuring lists would be a much more useful application var x, y = [1, 2]; or var tmp = [1, 2] ... var x, y = tmp; would be far more useful.
- tmcw 15y agoWell, lucky for you in Javascript 1.7: https://developer.mozilla.org/en/New_in_JavaScript_1.7#Destructuring_assignment_(Merge_into_own_page.2Fsection) https://developer.mozilla.org/en/New_in_JavaScript_1.7#Destr... (though I kinda feel like panning syntax sugar as 'useless' is a little useless. What, are we going to count chars as a scientific comparison?)
- magicalist 15y agoeven more excitingly, destructuring has been moved into the draft ECMAScript 6 spec: http://wiki.ecmascript.org/doku.php?id=harmony:destructuring http://wiki.ecmascript.org/doku.php?id=harmony:destructuring
- RegEx 15y ago> The comma in its typical uses in variable assignment is pretty useless IMO. I've screwed myself over a few times by getting into the habit of using the comma in this fashion. If you accidentally make that comma a semicolon, y in your example becomes global. My jQuery plugins all include a default settings object, and they happen to have the same name of "settings". Consequently, default settings for some plugins were being overridden and I had no idea why - until I found that I used a semicolon instead of a comma.
- knieveltech 15y agoThe JavaScript Comma Operator: bringing perl-esque side-effects to your code since....wait, has this thing always been an available operator? Whatever. The JavaScript Comma Operator: bringing perl-esque side effects to your code since it's introduction. There. Ahem.
- jleader 15y agoTo give credit where credit is due, Perl got the comma operator from C. A quick glance at their manuals suggests that neither BCPL (http://cm.bell-labs.com/who/dmr/bcpl.html http://cm.bell-labs.com/who/dmr/bcpl.html) nor B (http://cm.bell-labs.com/cm/cs/who/dmr/kbman.html http://cm.bell-labs.com/cm/cs/who/dmr/kbman.html) had it.
- deleted 15y ago[deleted]
- SoftwareMaven 15y agolhs && rhs lhs || rhs lhs , rhs Given the way js evaluates these, the comma operator seems like it should fit in nicely. I would have made this the first argument in my case for using the comma.
- nraynaud 15y agoI guess this syntax will now enter my team code-style guideline, as forbidden. It will nice along the ?: used as a control structure instead of selection.
- alain94040 15y agoIn hardware (Verilog), we use ?: all the time. Why not in JS?
- jallmann 15y agoThe ternary operator has its uses, such as in assignment. var foo = bar ? a : b;
- JoelMcCracken 15y agoThat's what he means by control structure instead of selection.
- JoelMcCracken 15y agoWhy is this considered so ugly? Is this not the same operation as var x=1, y=2, z=3; which is very, very common for variable initialization?
- xentronium 15y agoNope. It's the same operation as in: for (a = 0, b = 1; ... ; ...)
- olavk 15y agoNo, the comma in the var statement is not an operator, just a delimiter. But if you did: var a = (1, b = 2, c = 3); You would be using the comma operator. But that would be stupid.
- jpr 15y agoOh ffs did they have to copy all the retarded features of C's syntax into JS?
- olavk 15y agoNot that useful in ordinary code, but useful in code-transformation because you can replace a single expression with multiple expressions.
- tripzilch 15y agoThe comma operator is a bad idea in most cases, but his idea of using it to unobtrusively insert a `console.log` debug statement inside an expression, is pretty clever and might actually be a sort of valid use for this language feature. Also the way it allows one to formulate a `do { .. } while ( .. )` construction, is a nice and clever trick, but already violating the principle of least astonishment too much.