JavaScript Advanced Classes

Learn JavaScript Advanced Classes with practical examples, explanations, best practices, and real-world coding techniques.

Try Editor

Classes are a modern JavaScript feature introduced in ES6 that provide a cleaner and more organized way to create objects and reusable blueprints.

Classes simplify object-oriented programming in JavaScript and make code easier to structure, especially in large applications.

JavaScript classes are heavily used in:

  • Frontend frameworks
  • Backend applications
  • Reusable UI components
  • State management systems
  • Application architecture

Although classes look similar to classes in other programming languages, JavaScript classes are internally built on top of prototypes.

What is a Class?

A class is a blueprint for creating objects.

It defines:

  • Properties
  • Methods
  • Behavior

Multiple objects can be created from the same class.

This improves code reuse and consistency.

Creating a Basic Class

Classes are created using the class keyword.


class User {

  constructor(name, role) {

    this.name = name;

    this.role = role;

  }

  greet() {

    console.log(
      `Hello ${this.name}`
    );

  }

}

const user1 =
  new User(
    "Alex",
    "Developer"
  );

user1.greet();
      

The constructor() method initializes object properties when a new object is created.

Understanding the constructor Method

The constructor is a special method that runs automatically when an object is created using the new keyword.

It is commonly used to initialize object data.


class Product {

  constructor(name, price) {

    this.name = name;

    this.price = price;

  }

}

const laptop =
  new Product(
    "Laptop",
    1200
  );

console.log(laptop.name);
      

Class Methods

Methods define behavior for objects created from a class.

Methods are shared between all instances, making classes memory efficient.


class User {

  constructor(name) {

    this.name = name;

  }

  login() {

    console.log(
      `${this.name} logged in`
    );

  }

}

const user =
  new User("Sarah");

user.login();
      

Creating Multiple Objects

A single class can create many objects with different data.


class Car {

  constructor(brand) {

    this.brand = brand;

  }

}

const car1 =
  new Car("Toyota");

const car2 =
  new Car("Tesla");

console.log(car1.brand);
console.log(car2.brand);
      

Each object has its own independent property values.

Class Inheritance

Classes support inheritance using the extends keyword.

Inheritance allows one class to reuse functionality from another class.


class Animal {

  walk() {

    console.log("Walking");

  }

}

class Dog extends Animal {

  bark() {

    console.log("Woof");

  }

}

const pet =
  new Dog();

pet.walk();
pet.bark();
      

The Dog class inherits methods from Animal.

Using super()

The super() function calls the parent class constructor.

It is required when extending classes with constructors.


class Animal {

  constructor(name) {

    this.name = name;

  }

}

class Dog extends Animal {

  constructor(name, breed) {

    super(name);

    this.breed = breed;

  }

}

const pet =
  new Dog(
    "Rocky",
    "Labrador"
  );

console.log(pet.name);
      

Private Fields

Modern JavaScript supports private class fields using the # symbol.

Private fields cannot be accessed directly outside the class.


class BankAccount {

  #balance = 0;

  deposit(amount) {

    this.#balance += amount;

  }

  getBalance() {

    return this.#balance;

  }

}

const account =
  new BankAccount();

account.deposit(1000);

console.log(
  account.getBalance()
);
      

Encapsulation helps protect internal data from accidental modification.

Static Methods

Static methods belong to the class itself instead of object instances.


class MathHelper {

  static add(a, b) {

    return a + b;

  }

}

console.log(
  MathHelper.add(5, 3)
);
      

Static methods are commonly used for utility functionality.

Classes and Prototypes

Even though classes provide modern syntax, JavaScript still uses prototypes internally.

Class methods are stored on the prototype instead of being duplicated for every object.

This improves memory efficiency.

Real-World Use Cases

Classes are widely used in:

  • UI component systems
  • Game engines
  • Backend services
  • Reusable application modules
  • State management systems

Large applications often organize logic using classes and objects.

Common Beginner Mistakes

  • Forgetting to use the new keyword
  • Confusing instance methods with static methods
  • Overusing inheritance unnecessarily
  • Not understanding this inside class methods

Best Practices

  • Keep classes focused and reusable
  • Use meaningful method names
  • Prefer composition when inheritance becomes too complex
  • Use private fields for sensitive internal state

Why Classes Matter

Classes provide a clean and scalable way to organize large JavaScript applications.

They simplify object-oriented programming and improve readability in complex systems.

Most modern frameworks and enterprise applications use class-based architecture in some form.

Summary

Classes are reusable blueprints for creating objects in JavaScript. They simplify object-oriented programming by organizing properties, methods, inheritance, and encapsulation into a clean structure.

Understanding classes is essential for building scalable, maintainable, and professional JavaScript applications.

Code Examples

js

class User {

  constructor(name, role) {

    this.name = name;

    this.role = role;

  }

  greet() {

    console.log(
      `Hello ${this.name}`
    );

  }

}

const user1 =
  new User(
    "Alex",
    "Developer"
  );

user1.greet();

    
js

class Dog extends Animal {

  constructor(name, breed) {

    super(name);

    this.breed = breed;

  }

}

    
js

class BankAccount {

  #balance = 0;

  deposit(amount) {

    this.#balance += amount;

  }

  getBalance() {

    return this.#balance;

  }

}

    

Pro Tip

Even though classes provide modern syntax, JavaScript still uses prototype-based inheritance internally behind the scenes.

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.