The class declaration

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 ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.