Classes in ES6 Javascript

Javascript 9 min min read Updated: Mar 09, 2026 Intermediate
Classes in ES6 Javascript
Intermediate Topic 11 of 15

ES6 introduced the concept of classes in JavaScript, providing a cleaner and more structured way to create objects and implement object-oriented programming. Classes act as templates that allow developers to create multiple objects with similar properties and methods.

Before ES6, JavaScript used constructor functions and prototypes to create objects. ES6 classes simplify this process and make the syntax easier to understand.

What is a Class?

A class is a blueprint used to create objects. It defines the properties and methods that objects created from the class will have.

Key Point: A class is a template used to create objects with predefined properties and methods.

Creating a Class

In ES6, classes are defined using the class keyword.

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

The constructor() method is a special function that runs automatically when a new object is created.

Creating an Object from a Class

Objects are created from a class using the new keyword.

javascript class Person { constructor(name, age){ this.name = name; this.age = age; } } let user = new Person("Rahul", 22); console.log(user.name);
Output

Rahul

Adding Methods to a Class

Methods can be defined inside a class to perform specific actions.

javascript class Person { constructor(name){ this.name = name; } greet(){ console.log("Hello " + this.name); } } let user = new Person("Rahul"); user.greet();
Output

Hello Rahul

Key Point: Methods defined inside a class are shared by all objects created from that class.

Class Inheritance

Inheritance allows one class to inherit properties and methods from another class. ES6 uses the extends keyword for inheritance.

javascript class Animal { speak(){ console.log("Animal makes a sound"); } } class Dog extends Animal { bark(){ console.log("Dog barks"); } } let pet = new Dog(); pet.speak(); pet.bark();
Output

Animal makes a sound

Dog barks

Advantages of ES6 Classes

  • Provides cleaner syntax for object creation
  • Makes object-oriented programming easier
  • Supports inheritance
  • Improves code organization

Conclusion

Classes in ES6 provide a structured way to create objects and implement object-oriented programming in JavaScript. By using classes, constructors, and methods, developers can build scalable and maintainable applications.

ES6 classes simplify the process of creating objects and managing complex code structures in modern JavaScript development.

In the next tutorial, you will learn about JavaScript Modules (Import and Export), which allow developers to organize code into reusable files.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators