03 / 03

What is the purpose of the `super` keyword in JavaScript classes?

Difficulty: 5/10
class inheritance, method overriding, constructor chaining

The keyword in JavaScript classes serves an important role in enabling interaction between a subclass (child class) and its superclass (parent class). It is primarily used in two scenarios:

prototype
  1. 1

    In a subclass, you use to call the constructor of the parent class. This ensures that the parent's initialization logic runs, allowing the child class to inherit and utilize properties defined in the parent class

  2. 2

    If you don’t call super() in the subclass constructor, JavaScript will throw an error because the parent class constructor must be invoked.

  3. 3

    super() must be called before accessing in the subclass constructor.

  4. 4

    You use to call methods from the parent class within the subclass. This is useful when you want to extend or override functionality while still using the parent class's implementation.

javascript

Scenario Questions

0-2 years experience

  1. 1You need to create a subclass `Dog` that extends `Animal` and wants to reuse the parent constructor to set `name`. How would you use `super` in the `Dog` constructor?
  2. 2If you override a `speak()` method in a child class but still want to run the parent’s `speak()`, what syntax would you write and why?
  3. 3What error do you get if you omit `super()` in a subclass constructor that defines its own constructor, and why does it occur?

2-5 years experience

  1. 1We have a `User` base class with a `save()` method that logs to a database. A `PremiumUser` subclass overrides `save()` to also send a welcome email, but the code throws `this.save is not a function`. Walk me through why and how to fix it using `super`.
  2. 2During a refactor we moved shared initialization logic into the parent class constructor. Some existing subclasses now break. Explain how you would adjust the subclass constructors with `super` to keep their behavior.
  3. 3Discuss the trade‑offs of calling `super` at the start versus the end of a subclass constructor when you need to set up subclass‑only properties.

5-8 years experience

  1. 1Our front‑end framework provides base components with lifecycle methods. A team wants to extend a base component and override `componentDidMount` but still run the base logic. How would you design the inheritance and use `super` to ensure correct ordering and avoid performance regressions?
  2. 2We discovered that some classes call `super.method()` inside a tight loop, causing unnecessary work. How would you identify and refactor this pattern while preserving behavior?
  3. 3When using mixins that also call `super` inside methods, method resolution order can become ambiguous. Discuss how you would structure class hierarchies or use composition to avoid super‑related bugs at scale.

8+ years experience

  1. 1Our organization is migrating from prototype‑based inheritance to ES6 classes across multiple services. What guidelines would you set for using `super` to minimize breaking changes, especially concerning constructor signatures and method overrides?
  2. 2Consider a micro‑frontend architecture where shared UI components are extended by different teams. How would you define a contract for `super` usage to ensure backward compatibility and avoid tight coupling?
  3. 3If you need to support legacy browsers that don’t understand `super`, what strategies would you employ at the build or runtime level to preserve functionality across the whole platform?

Follow-up Questions

  • What would happen if you called `super` after accessing `this` in the constructor?
  • Can you think of a scenario where using `super` could introduce a subtle bug?
  • How does `super` behave differently for static methods versus instance methods?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.