Deathkamp Drone
Deathkamp Drone

Reputation: 282

Is the comma in a `var` statement an example of the comma operator?

Or in a const or let statement, for that matter.

For example, in this statement const a = 0, b = 1;, the equivalent code (completely same effect) would be:

const a = 0;
const b = 1;

But then, am I right to say that the comma in that statement is not the comma operator? Because if it was the comma operator, then const a = 0, b = 1 should be equivalent to:

a = 0; // This already would throw a ReferenceError in strict mode
const b = 1;

Similarly, var a, b, c; should be to a; b; var c; if that comma indeed was the comma operator, but instead its effect is that of var a; var b; var c;.

Is my reasoning correct, or do I misunderstand the comma operator? I ask this because I've seen multiple articles giving statements like var i = 0, j = 9; as examples of the comma operator (that last one is from the MDN page), but I think that it's a different comma, in the same way that commas in function parameters and arrays are different from the comma operator.

Is it an example of the comma operator, or not?

Upvotes: 0

Views: 87

Answers (1)

georg
georg

Reputation: 215009

Commas are used in quite a few places:

  • expressions: (x, y) + 1
  • declaration statements: let x, y
  • function arguments: foo(1,2,3)
  • array/object initializers: [1,2,3]
  • destructuring patterns [x, y] = a

Of these, only the first one is the comma operator, others are... well, just commas (although sometimes they go by a fancy name "elision").

For the let/const statements specifically, the grammar is here: http://www.ecma-international.org/ecma-262/8.0/#sec-let-and-const-declarations

Upvotes: 1

Related Questions