JavaScript Notes
Complete guide to OOP in JavaScript: classes, objects, the 4 pillars (encapsulation, inheritance, abstraction, polymorphism), prototype chain, and interview Q&A.
Object-Oriented Programming (OOP) is a programming paradigm that organizes code around objects — bundles of related data and behavior — rather than around functions and logic. JavaScript supports OOP through its prototype system and the modern class syntax.
OOP makes programs easier to understand, maintain, extend, and debug by modeling real-world concepts as objects.
📊 OOP Core Concepts Map
💻 Example 1 — Objects: Data + Behavior Together
// Without OOP — scattered data and functions
const studentName = "Riya";
const studentGrade = 92;
function getLetterGrade(grade) { return grade >= 90 ? "A" : "B"; }
function introduce(name, grade) { return `${name}: ${getLetterGrade(grade)}`; }
// With OOP — bundled together, self-contained
class Student {
constructor(name, grade) {
this.name = name;
this.grade = grade;
}
getLetterGrade() { return this.grade >= 90 ? "A" : "B"; }
introduce() { return `${this.name}: ${this.getLetterGrade()}`; }
}
const s = new Student("Riya", 92);
console.log(s.introduce());
console.log(s.getLetterGrade());Riya: A A
💻 Example 2 — The Four Pillars in One Example
10500 78.54 16.00 28.27
💻 Example 3 — Complete Real-World OOP Application: Task Manager
✅ [HIGH] Learn JavaScript OOP ⏳ [MEDIUM] Build a project ⏳ [HIGH] Practice interview questions Total: 3 | Done: 1 | Pending: 2
📋 OOP vs Functional Programming
| Aspect | OOP | Functional |
|---|---|---|
| Core unit | Objects | Functions |
| State | Mutable (controlled by methods) | Immutable data preferred |
| Code reuse | Inheritance, composition | Higher-order functions, composition |
| Best for | Modeling real-world entities | Data transformation pipelines |
| JavaScript support | Classes, prototypes | Map/filter/reduce, closures |
| Paradigm | Imperative | Declarative |
JavaScript supports both paradigms — choose what fits the problem.
⚠️ Common Mistakes
❌ Mistake 1 — Over-Engineering with OOP for Simple Problems
❌ Mistake 2 — Making Everything a Global Object
// BAD — tightly coupled global state
window.currentUser = { name: "Riya", loggedIn: true };
// GOOD — encapsulate in a class
class UserSession {
#user = null;
login(user) { this.#user = user; }
logout() { this.#user = null; }
get current(){ return this.#user; }
}❌ Mistake 3 — Mistaking Object Literals for OOP
Object literals are fine for simple data structures but they don't give you inheritance, encapsulation, or the ability to create many instances with shared behavior. Use classes for that.
🎯 Key Takeaways
- OOP organizes code around objects that bundle data and behavior
- The four pillars are: Encapsulation, Inheritance, Abstraction, Polymorphism
- A class is a blueprint;
new ClassName()creates an instance (object) - JavaScript OOP is prototype-based —
classis clean syntax over prototypes - Use OOP when modeling real-world entities with state and behavior
- Combine OOP and functional programming — JavaScript supports both
- Start simple: don't add classes until you have a clear reason to
❓ Interview Questions
Q1. What is OOP and why is it useful? > OOP is a paradigm that organizes code around objects — bundling related data and behavior together. It makes code more readable, reusable, and maintainable by modeling real-world entities as self-contained units.
Q2. What are the four pillars of OOP? > 1. Encapsulation — protect data by restricting direct access. 2. Inheritance — child classes reuse and extend parent class behavior. 3. Abstraction — hide complexity, expose clean interfaces. 4. Polymorphism — different objects respond to the same method call in different ways.
Q3. Is JavaScript truly object-oriented? > JavaScript is a multi-paradigm language. It supports OOP via prototype-based inheritance and ES6 classes, but also functional programming. Unlike Java or C++, JavaScript's OOP is prototype-based, not class-based — class is syntactic sugar over prototypes.
Q4. What is the difference between a class and an object? > A class is a blueprint (definition) — it does not occupy runtime memory by itself. An object (instance) is created from the class using new — it is a real value in memory with its own state.
Q5. What is a prototype in JavaScript OOP? > Every JavaScript object has an internal [[Prototype]] link to another object. Methods defined on a class are stored on its prototype object and shared by all instances via this chain. This is how JavaScript implements inheritance without copying methods into every object.
Q6. What is the difference between null and undefined in the context of OOP? > In OOP, null is often used to intentionally signal "no object here" (e.g., this.currentUser = null after logout). undefined signals a property has not been set at all. Using null makes intent clearer.
Q7. When should you use OOP vs functional programming in JavaScript? > Use OOP when you have entities with state and identity (User, Account, Task, Car). Use functional programming for data transformation pipelines (processing arrays, pure computations). JavaScript lets you mix both — use whatever best fits each problem.
Q8. What is the difference between class-based and prototype-based OOP? > In class-based OOP (Java, C++), classes are rigid blueprints, and instances are copies. In prototype-based OOP (JavaScript), objects inherit directly from other objects via prototype links. ES6 classes provide a class-like syntax, but the underlying mechanism is still prototype delegation.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Object-Oriented Programming in JavaScript – OOP Concepts Complete Guide.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, oop, object, oriented
Related JavaScript Master Course Topics