educative.io

Using commas inside classes constructors


Course: https://www.educative.io/collection/10370001/5862304337887232
Lesson: https://www.educative.io/collection/page/10370001/5862304337887232/6184140229574656
In the example given by the educative team inside the constructor function are used commas, looking at MDN I see they use semicolons.

I thought that using commas should generate a syntax error but it doesn’t. Could anyone explain how using commas is a correct syntax? Here is the example code

class Shape{
  constructor(name,sides,length){
    this.name = name, // shouldn't this comma be a ; ???
    this.sides = sides,
    this.length = length
  }
  static displayName() {
  return this.name
  }
}
// .... omitted

Hello @Giuseppe_Schembri,
It’s best always to remain consistent with convention. It’s either all semi-colons or 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.

Semicolons are optional in JS. However, it is good to put semicolons after each statement.
The class declaration is not a statement however, you can still put a semicolon after it.