Lesson 29 / 40

Classes & Constructors

Create objects with the class keyword and constructors, and see how classes sit on top of prototypes.

By · 15 years in web development · Updated 1 Oct 2026

class and constructor

constructor runs automatically when you create an object with new, setting up its initial properties.

class Dog {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  bark() {
    return `${this.name} says Woof!`;
  }
}
const rex = new Dog("Rex", 3);
console.log(rex.bark());

Classes are syntax sugar

Under the hood, JS classes still use prototypes — class is a cleaner syntax over the same prototype-based system.

The new keyword

Forgetting new before a class constructor throws a TypeError — classes can't be called like regular functions.