To define a class using the class declaration, you need to use the class keyword and a name for the class.
Here is a code example to demonstrate how to define a class using the class declaration:
class Student { constructor(name) { this.name = name; } } const s1 = new Student("Eden"); console.log(s1.name); //Output "Eden"
Here, we created a class named Student. Then, we defined a constructor method in it. Finally, we created a new instance of the class—an object, and logged the name property of the object.
The body of a class is in the curly brackets, that is, {}. This is where we need to define methods. Methods are defined without the function keyword, and a comma is not used in between the methods.
Classes are ...