Syntax of JavaScript classes

There are two ways to define a JavaScript class

The first way is by the so-called class declaration. In order to define a class with this way, you start by the keyword class followed by a name.

Example:

class Dishes {
constructor(category, price) {
    this.category = category;
    this.price = price;
  }
}

As you see, inside the class we have the constructor function. Inside the constructor is the place where we will initialize the properties of the object.

The constructor function is compulsory in the JavaScript classes. Even if you don't put it, the JavaScript will automatically create an empty one for you.

The second way to define a JavaScript class is by using the class expression. To do that, we create a variable to which we assign our class. A class expression can either be named or not.

Example of a named class expression:

 let myDishes = class Dishes {
constructor(category, price) {
    this.category = category;
    this.price = price;
  }
};

Example of unnamed class expression:

 let myDishes = class {
constructor(category, price) {
    this.category = category;
    this.price = price;
  }
};