A JavaScript subclass is a class that extends another class. The parent class provides shared properties and methods, while the subclass adds or overrides behavior. Modern JavaScript uses the extends keyword and super() for class inheritance.
Subclassing is useful when objects share a common base but need specialized behavior, such as Animal and Dog, User and AdminUser, or Shape and Circle.
Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.
Quick reference
| Need | Syntax / rule |
|---|---|
| Declare a subclass | class Child extends Parent { } |
| Initialize parent state | Call super(...) before using this in the child constructor |
| Call a parent method body | super.methodName() from the subclass method |
| Type checks | instance is instanceof both child and parent |
Method 1: Create a subclass with extends
Dog inherits Animal’s constructor via implicit super when you do not define your own constructor, and overrides speak().
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("subclass-speak:", dog.speak());You should see one line logging subclass-speak: Rex barks.
Method 2: instanceof parent and child
The same dog value satisfies both Dog and Animal in instanceof checks.
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("subclass-instance:", dog instanceof Animal && dog instanceof Dog);You should see one line logging subclass-instance: true.
Method 3: Subclass constructor and super()
You must call super(...) before touching this so the parent constructor runs first.
class User {
constructor(name) {
this.name = name;
}
}
class AdminUser extends User {
constructor(name, permissions) {
super(name);
this.permissions = permissions;
}
}
const admin = new AdminUser("Ana", ["read", "write"]);
console.log("admin:", admin.name, admin.permissions.join(","));You should see one line logging admin: Ana read,write.
Method 4: Override and delegate with super.speak()
super.speak() runs the parent implementation; the subclass can wrap or transform the result.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return `${this.name} makes a sound`;
}
}
class LoudDog extends Animal {
speak() {
return super.speak().toUpperCase();
}
}
console.log("loud:", new LoudDog("Rex").speak());You should see one line logging loud: REX MAKES A SOUND.
Common questions about JavaScript subclassing
How do I create a subclass in JavaScript?
Use class Child extends Parent { ... } and call super() in the constructor when the subclass defines one.
What does super do in JavaScript?
super() calls the parent constructor. super.methodName() calls a parent method.
Can a subclass override parent methods?
Yes. Define a method with the same name in the subclass.
Summary
Use JavaScript extends to create a subclass and super() to call the parent constructor. Subclasses inherit parent behavior, can override methods, and still pass instanceof checks for the parent class. Use subclassing when shared behavior is real and the child class truly is a specialized version of the parent.
