Given the following code
var fn = function () {
var x = 'x',
y = 'y';
this.a = 'a',
this.b = 'b',
this.c = 'c';
this.d = 'd',
this.e = 'e';
}
You can see that there is a mix of both.
What would be the benefit of using one or the other?
My understanding is that the semicolon is to end the statement. And comma should be used to string together multiple declarations.
So is it safe to say that with this example then there should only be two semicolon?
var fn = function () {
var x = 'x',
y = 'y';
this.a = 'a',
this.b = 'b',
this.c = 'c',
this.d = 'd',
this.e = 'e';
}
No, comma has three meanings.
var
before each variable.Example for two;
MDN
When is the comma operator useful?
It's best to always remain consistent with convention. It's either all semi-colons or all commas, and I'm sure you'd prefer to use semi-colons rather than commas everywhere.
There is no speed gain as well, so there's nothing to worry about.
It really doesn't matter. There's no speed benefit to using commas as far as I know.
Just use whatever you prefer :)
This is what JavaScript - The Good Parts has to say:
The comma operator is an operator that can be used inside an expression. It is used to separate out multiple different expressions and has the meaning "evaluate all of the following expressions, then produce the value of the final expression." For example:
means "evaluate
a = 1
, thenb = 2
, thenc = 3
, then evaluate to the value of the expressionc = 3
.The semicolon is not an operator and cannot be used inside an expression. It is used as part of JavaScript syntax to mark the end of an expression that is being treated as a statement. For example, you could say
And this would mean "there are three statements to do in sequence: evaluate the first expression as the first statement, the second expression as the second statement, and the third expression as the third statement."
In this regard, the two are not completely interchangeable. For example, you cannot write
Because
var a = 1
andvar b = 2
are statements, not expressions, and thus can't be separated by commas. You would have to use a semicolon here.(A note: you could say
because the language specifically permits this use of comma as a part of the syntax of a declaration statement. Here, comma is not used as an operator.)
Similarly, you can't say
Because here the right-hand side of the expression must be an expression, not a statement, and
;
is used to separate statements. The inner semicolon would have to be a comma instead. (Then again, this code is pretty awkward and unusual in the first place, so you probably shouldn't do this at all!)From a stylistic perspective, the comma operator is rarely used and is obscure enough that it might trip up reasonably competent JavaScript coders. As a result, I would strongly suggest not using it and instead following the established conventions in JavaScript about using semicolons to terminate statements, even if it would be equivalent and syntactically legal to use commas to separate out expressions that are each used as statements.
Hope this helps!