Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

extends — class inheritance

The `extends` keyword creates a derived class. When given a base constructor, it links the class to it through prototype chains. This lets the class inherit and override methods, and `super` lets it call the base constructor or inherited methods.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

class Derived extends Base { /* class body */ }

extends specifies what a class inherits from in a class declaration or expression: class Derived extends Base {}. It is a reserved word, so it cannot be a variable name. Inheritance does not copy methods: with ordinary inheritance, Derived.prototype inherits from Base.prototype, and the constructor Derived itself inherits from Base, also making inherited static methods available.

A class has at most one direct superclass. It has none if extends is omitted or if it uses extends null. The expression after extends must evaluate to a constructor or null. The constructor can be a class or a constructible function, and its prototype property must be an object or null. A plain object or arrow function after extends causes a TypeError. With extends null, Derived.prototype has no parent prototype and the default constructor cannot create instances; a custom constructor that returns an object is needed.

In a custom constructor for a derived class with a base constructor, call super(...) before accessing this. It invokes the base constructor and initializes this. An exception is a constructor that explicitly returns an object without using this. If you omit the derived constructor, the default constructor forwards the arguments to the base constructor.

A method with the same name overrides an inherited method; super.describe() calls the base version with the current this. In the example, Dog extends the result of Animal.describe(), and both instanceof checks on dog return true.

Examples

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

  describe() {
    return this.name;
  }
}

class Dog extends Animal {
  constructor(name) {
    super(name);
  }

  describe() {
    return super.describe() + " barks";
  }
}

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

Result

Rex barks
true
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources