3

I'm working on some jQuery code that a previous developer wrote. I am trying to understand when/why a person would use commas to separate statements as opposed to semicolons.

In the below example, is it safe to say that there is no benefit to using a comma to separate the statements? If yes...why, if no... is there a situation in which it would make sense to use commas?

FYI: This questions is about chaining statements and whether or not there is a benefit. Not about what the comma does in the statements.

var $some_field_1, $some_child_field, $some_field_2, $some_child_field_2;

function a() {

    $some_field_1 = jQuery(".field_class"), $some_child_field = $some_field_1.find("input"), RunSomeFunction(), $some_field_2 = jQuery(".field_class_2"), $some_child_field_2 = $some_field_2.find("input");

}

VS.

var $some_field_1, $some_child_field, $some_field_2, $some_child_field_2;

function a() {

    $some_field_1 = jQuery(".field_class");

    $some_child_field = $some_field_1.find("input");

    RunSomeFunction();

    $some_field_2 = jQuery(".field_class_2");

    $some_child_field_2 = $some_field_2.find("input");

}

I hope this makes sense. Thanks all!

TKEz
  • 77
  • 8
  • Possible duplicate of [What does a comma do in JavaScript expressions?](http://stackoverflow.com/questions/3561043/what-does-a-comma-do-in-javascript-expressions) – CBroe Aug 10 '16 at 21:48
  • 1
    They're both terrible examples... neither uses `var` `const` or `let` resulting in global vars. – Kevin B Aug 10 '16 at 21:49
  • 1
    There is no difference as you put it, but if there is a `var` keyword preceding that list of assignments, it makes a difference (same for `let`, `const`). – trincot Aug 10 '16 at 21:49
  • @CBroe Not necessarily a duplicate, this question is asking about chaining statements – Andrew Li Aug 10 '16 at 21:49
  • @KevinB That's not all of the code. The above statements are within a function. In the global scope the variables are defined with var. Sorry I should have been more specific. – TKEz Aug 11 '16 at 16:08
  • even within a function, lacking `var` `const` or `let` will still create a global var. – Kevin B Aug 11 '16 at 16:10

1 Answers1

6

Normally commas are used to separate out variable declarations, but the version you have shown is using them to separate out expressions. While this is nuanced, it is still significant.

Note that all of the expressions are assigning variable names to the global scope (technically the Lexical Environment in the case of your example, it is possible those variables are defined in a more local scope than the global one).

A better example would have been

var one = 1, two = 2, sum = function(a,b){ return a+b; };

Note that if you tried to call a function in the variable declaration here it would have caused an Uncaught SyntaxError: Unexpected token (.

As all of the declarations are inherently prefaced by the var and are therefore local variables (in the Variable Environment).

Contrast the example with

one = 1, two = 2, sum = function(a,b){ return a+b; }, sum(one,two);

which is essentially your example in the question, and the difference is mostly the scope of the variables and also that as each is simply an expression, the function call is allowed.

More on the Comma operatorMDN

Travis J
  • 77,009
  • 39
  • 185
  • 250
  • Thanks! So just to be clear. When chaining statements with commas there is no real difference it's simply the preference of the developer? – TKEz Aug 11 '16 at 16:23
  • 1
    @TKEz - Chaining usually implies that some sort of values are being passed from one call to another. This is simply separation, if you are using a comma instead of a semicolon. In that case, it is up to you, however in my opinion it makes it harder to read if you use a comma to indicate the end of an expression or statement than a semi colon. – Travis J Aug 11 '16 at 17:11