Inherit JavaScript Class: Which Keyword to Use

Benjamin Jul 11, 2026

In the realm of JavaScript, inheritance is a fundamental concept that allows one class to acquire properties and methods from another. This not only promotes code reusability but also establishes a hierarchical relationship between classes. But what keyword do we use to inherit a class in JavaScript?

JavaScript this Keyword: Master Context Like a Pro πŸš€
JavaScript this Keyword: Master Context Like a Pro πŸš€

Before we dive into the keyword, let's briefly understand what inheritance is in JavaScript. Inheritance in JavaScript is a mechanism where a new class (subclass or derived class) can acquire properties and methods of an existing class (superclass or base class). This allows us to create a hierarchical relationship between classes, where the subclass can extend the functionality of the superclass.

Declaring a variable in JavaScript using the let keyword
Declaring a variable in JavaScript using the let keyword

Keyword for Inheritance in JavaScript

In JavaScript, the keyword used to inherit a class is the 'extends' keyword. It was introduced with the advent of ES6 (ECMAScript 2015) and has since become a cornerstone of JavaScript's class-based syntax.

10 Example of this keyword in Java
10 Example of this keyword in Java

The 'extends' keyword allows a class to inherit properties and methods from another class. It establishes a 'is-a' relationship between the two classes. For instance, if we have a 'Vehicle' class and a 'Car' class, we can use 'extends' to express that a 'Car' is a type of 'Vehicle'.

Syntax of 'extends' Keyword

Javascript classes
Javascript classes

The basic syntax of the 'extends' keyword is as follows:

class SubClass extends SuperClass {
  // class body
}

Here, 'SubClass' is the subclass that inherits from 'SuperClass'. The class body contains the properties and methods that belong to the subclass.

Inheriting Properties and Methods

The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had

When a class inherits from another class using the 'extends' keyword, it automatically gains access to all the properties and methods of the superclass. This includes both the data properties and the methods defined in the superclass.

Here's a simple example:

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

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

class Dog extends Animal {
  constructor(name, breed) {
    super(name);
    this.breed = breed;
  }

  speak() {
    console.log(`${this.name} says: Woof!`);
  }
}

In this example, the 'Dog' class inherits from the 'Animal' class. It gains access to the 'name' property and the 'speak' method from the 'Animal' class. Additionally, it defines its own 'breed' property and overrides the 'speak' method to provide its own implementation.

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Inheritance and the 'super' Keyword

In the previous example, we used the 'super' keyword to call the constructor of the superclass from within the subclass. The 'super' keyword is used to call functions on the superclass.

Java Final Keyword Cheat Sheet – Explained Simply
Java Final Keyword Cheat Sheet – Explained Simply
Java List of Keywords (All Reserved Words) | Complete Java Reference Guide
Java List of Keywords (All Reserved Words) | Complete Java Reference Guide
Grading System In Javascript With Source Code
Grading System In Javascript With Source Code
JavaScript Objects: The Key to Structured Data πŸš€
JavaScript Objects: The Key to Structured Data πŸš€
Examination Apps in JavaScript with Source Code
Examination Apps in JavaScript with Source Code
a whiteboard with different types of text on it and the words in each language
a whiteboard with different types of text on it and the words in each language
How to Declare Variables in JavaScript
How to Declare Variables in JavaScript
Data Types in JavaScript
Data Types in JavaScript
HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet
β€˜this’ KeyWord in JavaScript & How it Works
β€˜this’ KeyWord in JavaScript & How it Works
Keywords in JAVA
Keywords in JAVA
Object Oriented Programming (OOP) in #javascript using ES6 Objects & Inheritance #websitedevelopment
Object Oriented Programming (OOP) in #javascript using ES6 Objects & Inheritance #websitedevelopment
Java Programming
Java Programming
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
Data type in Javascript
Data type in Javascript
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
JavaScript Cheat Sheet πŸš€ | Complete JS Reference for Beginners & Developers
JavaScript Cheat Sheet πŸš€ | Complete JS Reference for Beginners & Developers
Dynamic Class Toggle in DOM
Dynamic Class Toggle in DOM
Student Management System In JavaScript With Source Code
Student Management System In JavaScript With Source Code
the different languages used in web development and programming are shown here, as well as text
the different languages used in web development and programming are shown here, as well as text

The 'super' keyword can also be used to call superclass methods from within the subclass. This can be useful when you want to extend the functionality of a superclass method, rather than completely overriding it.

Calling Superclass Methods with 'super'

Here's an example of how the 'super' keyword can be used to call a superclass method:

class Animal {
  speak() {
    console.log('The animal makes a sound.');
  }
}

class Dog extends Animal {
  speak(message) {
    super.speak();
    console.log(`The ${message} says: Woof!`);
  }
}

In this example, the 'speak' method in the 'Dog' class calls the 'speak' method of the 'Animal' class using the 'super' keyword. This allows the 'Dog' class to extend the functionality of the 'speak' method, rather than completely overriding it.

In the world of JavaScript, understanding and effectively using the 'extends' keyword for inheritance is crucial for building scalable and maintainable code. It allows us to create a clear hierarchy of classes, promoting code reuse and making our code more organized and easy to understand.

So, the next time you're building a JavaScript application and need to establish a hierarchical relationship between classes, remember the 'extends' keyword. It's your key to unlocking the power of inheritance in JavaScript.