JavaScript Classes — Interview Questions & Answers
Ques: What are classes in JavaScript?
Ans: A class is a template for creating objects. It simplifies working with objects and inheritance in an object-oriented way.
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello, ${this.name}`);
}
}
Ques: Why were classes introduced in ES6?
Ans: To make OOP (Object-Oriented Programming) more readable and structured. They provide cleaner syntax for prototype-based inheritance in JavaScript.
Ques: What is the difference between a class and a constructor function?
| Feature | Class | Constructor Function |
|---|---|---|
| Syntax | ES6 syntax (class) | ES5 function |
| Hoisting | Not hoisted | Hoisted |
| Strict mode | Always strict | Optional |
| Methods | Defined inside class body | Defined on prototype manually |
Ques: How do you create an instance of a class?
Ans: Use the new keyword:
class Car {
constructor(model) { this.model = model; }
}
const c1 = new Car("Tesla");
Ques: What is a constructor method?
Ans: A special method automatically called when a new object is created from a class.
class User {
constructor(name) {
this.name = name;
}
}
Ques: Can a class have multiple constructors?
Ans: No, A class can have only one constructor.
However, you can handle multiple initialization patterns using default or conditional logic.
Ques: What are instance methods and static methods?
- Instance methods: Belong to the object instance.
- Static methods: Belong to the class itself.
class MathUtils {
static add(a, b) { return a + b; }
multiply(a, b) { return a * b; }
}
MathUtils.add(2, 3); // 5
new MathUtils().multiply(2, 3); // 6
Ques: What is inheritance in classes?
Ans: It allows one class to derive properties and methods from another using extends.
class Animal {
speak() { console.log("Animal sound"); }
}
class Dog extends Animal {
speak() { console.log("Bark"); }
}
new Dog().speak(); // "Bark"
Ques: What is the role of super()?
Ans: Used inside the child class constructor to call the parent class constructor.
class Parent {
constructor(name) { this.name = name; }
}
class Child extends Parent {
constructor(name, age) {
super(name);
this.age = age;
}
}
Ques: What are getters and setters in classes?
Ans: They provide controlled access to object properties.
class User {
constructor(name) { this._name = name; }
get name() { return this._name.toUpperCase(); }
set name(value) { this._name = value; }
}
Ques: What are private fields in JavaScript classes?
Ans: Private fields (prefixed with #) are accessible only inside the class.
class BankAccount {
#balance = 0;
deposit(amount) { this.#balance += amount; }
}
Ques: What are public fields in classes?
Ans: They are declared directly in the class body and can be accessed from anywhere.
class Car {
color = "blue";
}
console.log(new Car().color); // "blue"
Ques: What is the extends keyword used for?
Ans: It sets up inheritance — allowing a subclass to access the parent’s properties and methods.
class Employee extends Person {}
Ques: Can you override parent class methods?
Ans: Yes, by redefining the same method name in the child class.
Ques: What happens if you forget to call super() in a subclass constructor?
Ans: You’ll get a ReferenceError, because this cannot be used before calling super() in derived classes.
Ques: What are mixins in JavaScript classes?
Ans: Mixins allow combining multiple behaviors into one class.
const canRun = {
run() { console.log("Running..."); }
};
class Animal {}
Object.assign(Animal.prototype, canRun);
Ques: What is the difference between prototype and class syntax?
Ans: Both create objects with shared methods. Classes are syntactic sugar over prototype-based inheritance.
Ques: What are best practices with JavaScript classes?
- Keep methods small and focused.
- Prefer composition over deep inheritance.
- Use private fields for encapsulation.
- Use getters/setters for data validation.
