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?

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.

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.

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

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

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.

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.




















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.