Advertisement

Classes & Class Syntax interview questions and answers

10 Classes & Class Syntax interview questions for JavaScript roles, each with a model answer, examples and likely follow-ups.

10 questions
Advertisement

10 questions

Q1 Beginner Save

What is a class in JavaScript, and is it a new kind of object model?

A class is syntactic sugar over JavaScript's existing prototype-based inheritance. It doesn't add a new object model — under the hood, methods still live on the prototype object, just written with cleaner syntax.
class Animal {
  constructor(name) { this.name = name; }
  speak() { return `${this.name} makes a sound.`; }
}
const a = new Animal('Rex');
console.log(a.speak());
Real-world example
Modeling domain entities like User, Order, or Product with clear, readable structure.
Likely follow-ups
  • Is a class hoisted the same way a function declaration is?
Prototypes & Inheritance
Q2 Beginner Save

How does the constructor method work in a class?

constructor() runs automatically when you call new ClassName(...), initializing instance properties. A class can have at most one constructor.
class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
}
const p = new Point(3, 4);
Real-world example
Setting up a new object's initial state, like a shopping cart starting with an empty items array.
Likely follow-ups
  • What happens if you don't define a constructor at all?
this & Binding
Q3 Beginner Save

How do you create a subclass using extends?

extends sets up prototype-based inheritance between classes, and super() inside the subclass's constructor calls the parent class's constructor to initialize inherited state before adding subclass-specific properties.
class Animal {
  constructor(name) { this.name = name; }
}
class Dog extends Animal {
  constructor(name, breed) {
    super(name);
    this.breed = breed;
  }
}
Real-world example
Modeling an AdminUser class that extends a base User class with extra permissions.
Likely follow-ups
  • Must super() be called before using 'this' in a subclass constructor?
Prototypes & Inheritance
Q4 Intermediate Save

What are class fields, and how do they differ from constructor-assigned properties?

Class fields let you declare and initialize instance properties directly in the class body, outside the constructor, which runs before the constructor body executes. They reduce boilerplate especially with inheritance and default values.
class Counter {
  count = 0; // class field
  increment() { this.count++; }
}
Real-world example
Declaring default component state fields cleanly at the top of a class, without cluttering the constructor.
Likely follow-ups
  • Are class fields defined before or after super() runs in a subclass?
Objects Property Descriptors & Immutability
Q5 Intermediate Save

What does the # syntax mean for class members?

The # prefix marks a truly private field or method, accessible only from inside the class body itself — not even subclasses or external code can read or call it, and accessing it from outside throws a SyntaxError, not just returning undefined.
class BankAccount {
  #balance = 0;
  deposit(amount) { this.#balance += amount; }
  getBalance() { return this.#balance; }
}
const acc = new BankAccount();
// acc.#balance; // SyntaxError outside the class
Real-world example
Hiding an account's internal balance so it can only be modified through controlled methods like deposit()/withdraw().
Likely follow-ups
  • How did developers fake privacy before # private fields existed?
Design Patterns in JavaScript
Q6 Intermediate Save

What are static methods and properties, and when do you use them?

static members belong to the class itself, not to instances — you call them as ClassName.method() rather than instance.method(). They're useful for factory methods, utility functions, or shared counters tied to the class as a whole.
class User {
  static count = 0;
  constructor() { User.count++; }
  static fromJSON(json) { return new User(JSON.parse(json)); }
}
Real-world example
A static User.fromJSON() factory method that builds a User instance from an API response.
Likely follow-ups
  • Can static methods access instance properties directly?
JSON & Data Serialization
Q7 Advanced Save

How do getters and setters work inside a class?

get and set define accessor properties that look like plain fields from the outside but run custom logic on read or write — useful for validation or computed values without changing how consumers interact with the object.
class Temperature {
  #celsius = 0;
  get fahrenheit() { return this.#celsius * 9/5 + 32; }
  set fahrenheit(f) { this.#celsius = (f - 32) * 5/9; }
}
const t = new Temperature();
t.fahrenheit = 98.6;
Real-world example
Exposing a computed fullName getter derived from firstName and lastName fields.
Likely follow-ups
  • Do getters/setters count as own properties when you use Object.keys()?
Objects Property Descriptors & Immutability
Q8 Advanced Save

How does method overriding with super work in JavaScript classes?

A subclass can redefine a parent's method by declaring one with the same name; calling super.methodName() from inside the override lets you still invoke the parent's original implementation, typically to extend rather than fully replace it.
class Shape {
  describe() { return 'A shape'; }
}
class Circle extends Shape {
  describe() { return super.describe() + ', specifically a circle'; }
}
new Circle().describe(); // 'A shape, specifically a circle'
Real-world example
Extending a base Logger class's log() method to add a timestamp prefix while still writing the original message.
Likely follow-ups
  • Can you call super outside of a class method?
Design Patterns in JavaScript
Q9 Advanced Save

What is a static initialization block and why would you use one?

A static {} block runs once when the class is defined, letting you run multi-step setup logic for static fields that a simple assignment can't express — including try/catch around initialization.
class Config {
  static settings;
  static {
    try {
      Config.settings = JSON.parse(loadConfigString());
    } catch {
      Config.settings = {};
    }
  }
}
Real-world example
Loading and validating shared configuration once when a class module is first imported.
Likely follow-ups
  • Can a class have multiple static blocks?
Error Handling
Q10 Advanced Save

Why can classes not be called without 'new', unlike regular functions?

Class constructors are designed to enforce proper object construction; calling a class without new throws a TypeError by specification, unlike ordinary functions which silently run with 'this' bound to undefined (in strict mode) or the global object.
class Foo {}
Foo(); // TypeError: Class constructor Foo cannot be invoked without 'new'
Real-world example
Preventing a common bug where a class is accidentally called like a plain function and silently misbehaves.
Likely follow-ups
  • How do factory functions differ from classes in this respect?
this & Binding