Course topics

By WebNest Studio Engineering Team

JavaScript Tutorial

JavaScript Classes

Classes provide a cleaner, more familiar syntax for creating objects and setting up inheritance in JavaScript, introduced in ECMAScript 2015 (ES6). A class defines a blueprint: a constructor for initializing new instances, and methods those instances can call.

It is important to understand that JavaScript classes are primarily "syntactic sugar" over the language's existing prototype-based inheritance model — under the hood, class methods still live on a prototype object, exactly as they did before classes existed. Classes simply make that mechanism easier to read and write.

Why it matters

Classes are the standard way to define objects that share behaviour, and you will meet them in libraries, in back-end frameworks such as NestJS, and in older React code. They are a cleaner syntax over JavaScript's prototype system, and understanding that connection is a frequent interview topic.

Class Syntax, Constructor, and Methods

A class is declared with the class keyword. The special constructor method runs automatically when a new instance is created with new, and is typically used to set initial property values. Any other method defined in the class body becomes available on every instance, without needing to be redefined for each one.

Inheritance with extends and super

A class can inherit from another using extends, gaining access to the parent class's methods and properties. Inside a subclass's constructor, super(...) must be called before using this, because it runs the parent class's constructor to properly initialize the inherited part of the object. super.methodName() can also be used inside a method to call the parent class's version of that method.

Classes as Syntactic Sugar Over Prototypes

Every JavaScript object has an internal link to another object called its prototype, and method lookups walk up this "prototype chain" when a property is not found directly on the object. When you write a class method, JavaScript places it on ClassName.prototype, exactly where a method manually attached with the older Constructor.prototype.method = ... pattern would go. extends similarly wires up the prototype chain between the parent and child classes. This means typeof MyClass is actually "function", confirming that a class is still a function under the hood.

Examples

A base class and a subclass using extends and super

JavaScript
class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    return `${this.name} makes a sound.`;
  }
}

class Dog extends Animal {
  constructor(name, breed) {
    super(name); // must run before using "this"
    this.breed = breed;
  }

  speak() {
    const base = super.speak();
    return `${base} Specifically, ${this.name} barks.`;
  }
}

const genericAnimal = new Animal("Creature");
const dog = new Dog("Rex", "Labrador");

console.log(genericAnimal.speak());
console.log(dog.speak());
console.log(dog instanceof Animal);
Output
Creature makes a sound.
Rex makes a sound. Specifically, Rex barks.
true

Confirming that a class method lives on the prototype, not on each instance

JavaScript
class Counter {
  constructor() {
    this.count = 0;
  }
  increment() {
    this.count += 1;
    return this.count;
  }
}

const c1 = new Counter();
const c2 = new Counter();

console.log(c1.increment());
console.log(c1.increment());
console.log(c2.increment()); // independent instance state

console.log(Object.getPrototypeOf(c1) === Counter.prototype);
console.log(c1.increment === c2.increment); // same shared function on the prototype
Output
1
2
1
true
true

Common Mistakes

  • Forgetting to call "super(...)" as the first statement in a subclass constructor before referencing "this", which throws a ReferenceError.
  • Trying to call a class without "new" (e.g. "MyClass()" instead of "new MyClass()") — classes cannot be invoked as plain functions and throw a TypeError.
  • Assuming each instance gets its own independent copy of every method — methods are shared on the prototype, only instance properties (like those set in the constructor) are per-instance.
  • Believing classes introduce an entirely new inheritance model — they are built on the same prototype chain that existed in JavaScript long before ES6 classes.

Exercise

Write a class Animal whose constructor stores a name, with a method speak. Write a class Dog that extends it and overrides speak to return the name followed by barks.

Expected output: Rex barks then true

JavaScript · starter code
// TODO: class Animal with a constructor(name) and speak()

// TODO: class Dog extends Animal, overriding speak()

const dog = new Dog("Rex");
console.log(dog.speak());
console.log(dog instanceof Animal);
Show hints
  • The subclass is declared with class Dog extends Animal. It inherits the constructor, so it does not need its own.
  • Inside a method, the name is read as this.name.
Show solution
JavaScript · solution
class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    return this.name + " makes a sound";
  }
}

class Dog extends Animal {
  speak() {
    return this.name + " barks";
  }
}

const dog = new Dog("Rex");
console.log(dog.speak());
console.log(dog instanceof Animal);

Quick quiz

1. In a subclass constructor, what must happen before this is used?

  • A. Nothing
  • B. A return statement
  • C. The class must be exported
  • D. super() must be called
Show answer

D. super() runs the parent constructor, which creates the object.

2. Where are the methods of a class stored?

  • A. In the constructor
  • B. Copied onto each instance
  • C. In the global scope
  • D. On the prototype, shared by all instances
Show answer

D. Each instance looks the method up through its prototype.

3. What does the static keyword do to a method?

  • A. Attaches it to the class itself, not to its instances
  • B. Makes it faster
  • C. Prevents it from being overridden
  • D. Makes it private
Show answer

A. It is called as ClassName.method(), not on an object.

Interview questions

Are JavaScript classes the same as classes in Java?

Show answer

No. They look similar, but JavaScript classes are syntax over prototypes. A class is a function, its methods are properties of its prototype object, and an instance finds a method by following its prototype chain at runtime. Inheritance with extends links one prototype to another. The class syntax adds conveniences such as super, static members and private fields written with #.

What is the prototype chain?

Show answer

Every object has an internal link to another object, its prototype. When a property is read and the object does not have it, JavaScript looks at the prototype, then at that object's prototype, and so on until it finds the property or reaches null. This chain is how objects inherit methods: an array finds map on Array.prototype, and toString further up on Object.prototype.

Key Points to Remember

  • A class's constructor initializes new instances; other methods are defined once and shared through the prototype.
  • "extends" sets up inheritance, and "super(...)" must run before "this" is used inside a subclass constructor.
  • Classes are syntactic sugar over prototype-based inheritance — class methods live on ClassName.prototype.
  • Classes must be instantiated with "new"; calling one directly as a function throws a TypeError.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering