Using commas versus semicolons in JavaScript?

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 mixture of both.
What is the use of using one or the other?

I understand that the semicolon ends. And the comma should be used to combine multiple declarations.

So can it be said that with this example then there should be only two semicolons?

 var fn = function () { var x = 'x', y = 'y'; this.a = 'a', this.b = 'b', this.c = 'c', this.d = 'd', this.e = 'e'; } 
+48
javascript syntax semicolon comma
Mar 18 '13 at 19:57
source share
4 answers

The comma operator is an operator that can be used inside an expression. It is used to highlight several different expressions and has the meaning of "evaluate all of the following expressions and then produce the value of the final expression." For example:

 a = 1, b = 2, c = 3 

means "evaluate a = 1 , then b = 2 , then c = 3 , then evaluate the value of the expression c = 3 .

The semicolon is not an operator and cannot be used inside an expression. It is used as part of JavaScript syntax to indicate the end of an expression that is considered an operator. For example, you could say

 a = 1; b = 2; c = 3; 

And this will mean "there are three operators in the sequence: evaluate the first expression as the first operator, the second expression as the second statement, and the third expression as the third operator."

In this regard, the two are not completely interchangeable. For example, you cannot write

 var a = 1, var b = 2; 

Because var a = 1 and var b = 2 are operators, not expressions, and therefore cannot be separated by commas. Here you must use a semicolon.

(Note: you could say

 var a = 1, b = 2; 

because the language specifically allows this comma to be used as part of the syntax of the declaration statement. Here the comma is not used as an operator.)

Similarly, you cannot say

 a = (b = 1; c = 2); 

Because here the right side of the expression should be an expression, not an expression, but ; used to separate operators. Instead, the internal semicolon should be a semicolon. (Again, this code is rather inconvenient and unusual in the first place, so you probably shouldn't do that!)

From a stylistic point of view, the comma operator is rarely used and not hidden enough for it to deploy JavaScript codecs that are competent enough. As a result, I would strongly recommend not using it and instead following the established conventions in JavaScript about using semicolons to complete statements, even if it would be equivalent and syntactically legal to use commas to highlight expressions that each of them is used as statements.

Hope this helps!

+61
Mar 18 '13 at 20:02
source share

No, the comma has three meanings.

  • With a variable declaration, it simply allows you to omit var before each variable.
  • With expressions: "The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand."
  • Separate arguments in function declarations and function calls, but this should be obvious to every programmer.

Example for two:

 alert((2,3)); //3 

Note that the comma in the var expression is not a comma operator because it does not exist in the expression. Rather, it is a special character in var statements to combine several of them into one. In practice, this comma behaves in much the same way as the comma operator.

MDN

When is the comma operator useful?

+26
Mar 18 '13 at 20:00
source share

It really doesn't matter. As far as I know, there is no benefit to using commas.

Just use what you prefer :)

Here is what JavaScript - The good parts to say:

The comma operator can lead to overly complex expressions. It can also mask some programming errors.

JSLint expects to see a comma used as a delimiter, but not as an operator (except for the initialization and increment parts for approval). He does not expect literals in the array. Extra commas should not be used. A comma should not appear after the last element of an array literal or object literal, because this may be misinterpreted by some browsers.

+9
Mar 18 '13 at 19:59
source share

It is best to always be in agreement with the convention. These are either all half-columns, or all commas, and I’m sure that you would prefer to use half-columns rather than commas everywhere.

There is also no increase in speed, so you have nothing to worry about.

+4
Mar 18 '13 at 19:59
source share



All Articles