JS Class Inheritance

8 min read

What is JS Class Inheritance?

In this lesson, we explore JS Class Inheritance. This is an important concept in JavaScript development that you will use frequently when building real-world projects. Understanding JS Class Inheritance will help you write cleaner, more efficient code.

Syntax & Usage

The syntax and usage patterns for JS Class Inheritance follow standard JavaScript conventions. Below you will see code examples that demonstrate the most common use cases. Try modifying these examples in your own editor to experiment with different approaches.

Example

Here is a practical example demonstrating JS Class Inheritance:

Tips & Best Practices

  • Use const by default, let when you need to reassign
  • Use strict equality (===) instead of loose equality (==)
  • Handle errors with try/catch blocks for robust code
  • Use template literals instead of string concatenation

Summary

In this lesson you learned about JS Class Inheritance. Practice writing code to reinforce these concepts and move on to the next lesson when you feel comfortable.

Examples

Example
javascript
class Animal {
  constructor(name, sound) {
    this.name = name;
    this.sound = sound;
  }

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

class Dog extends Animal {
  constructor(name) {
    super(name, 'Woof!');
  }

  fetch(item) {
    return `${this.name} fetches the ${item}`;
  }
}

const dog = new Dog('Rex');
console.log(dog.speak());       // Rex says Woof!
console.log(dog.fetch('ball')); // Rex fetches the ball
Output
Rex says Woof! Rex fetches the ball

Test Your Knowledge

Take a quick quiz to check what you've learned in this lesson.

JS Class Inheritance Quiz

1

Which keyword declares a variable that cannot be reassigned?

Explanation: const declares a variable whose value cannot be reassigned after initialization.
2

What does === check in JavaScript?

Explanation: The strict equality operator (===) checks both value and type without type coercion.
3

Which method adds an element to the end of an array?

Explanation: The push() method adds one or more elements to the end of an array.

Related articles