ES6
September 10, 2026 2 min read

Classes in ES6

HS
HEMANT SINGH
Technical Writer & Educator

Classes in ES6

A class is a blueprint for creating objects.

Classes were introduced in ES6 and make object-oriented programming easier to write and understand.

1. Creating a Class

Use the class keyword to create a class.

class Student {

}

Here, Student is the name of the class.

2. Constructor

A constructor is a special method that runs automatically when an object is created.

class Student {

constructor(name, age) {
this.name = name;
this.age = age;
}

}

The constructor() is used to initialize the object's properties.

3. Methods

A method is a function defined inside a class.

class Student {

constructor(name) {
this.name = name;
}

greet() {
console.log("Hello " + this.name);
}

}

Here, greet() is a method.

4. Creating Objects

Use the new keyword to create an object from a class.

let student1 = new Student("Hemant");

let student2 = new Student("Rahul");

Now we can use the object's method:

student1.greet();
// Hello Hemant

student2.greet();
// Hello Rahul

5. extends

extends is used to create a class that inherits from another class.

class Person {

greet() {
console.log("Hello");
}

}

class Student extends Person {

}

Here, Student inherits from Person.

6. Inheritance

Inheritance means one class can use the properties and methods of another class.

class Person {

constructor(name) {
this.name = name;
}

greet() {
console.log("Hello " + this.name);
}

}

class Student extends Person {

}

Now Student can use the name property and greet() method.

let student = new Student("Hemant");

student.greet();

Output:

Hello Hemant

7. super

super() is used to call the constructor of the parent class.

class Person {

constructor(name) {
this.name = name;
}

}

class Student extends Person {

constructor(name, course) {
super(name);

this.course = course;
}

}

Here:

super(name);

calls the Person constructor.

Then:

this.course = course;

adds a property specific to Student.

Complete Example

class Person {

constructor(name) {
this.name = name;
}

greet() {
console.log("Hello " + this.name);
}

}

class Student extends Person {

constructor(name, course) {
super(name);
this.course = course;
}

study() {
console.log(this.name + " is studying " + this.course);
}

}

let student = new Student("Hemant", "JavaScript");

student.greet();
student.study();

Output:

Hello Hemant
Hemant is studying JavaScript