Programming Paradigms
Explore the major programming paradigms — imperative, declarative, OOP, and functional — and when to use each.
What you'll learn
- What a programming paradigm is and why there are several
- Imperative vs. declarative: telling the computer how vs. what
- Object-oriented programming: encapsulation and objects
- Functional programming: pure functions and immutability
- How JavaScript supports multiple paradigms
Concept
Programming Paradigms
A programming paradigm is a style or philosophy of writing code. It is a set of principles that shapes how you structure your programs, how you organize data, and how you express logic. No paradigm is universally "best" — each is a lens that makes certain problems easier to solve and certain patterns more natural to express.
Imperative vs. Declarative
The most fundamental distinction is between imperative and declarative code:
- Imperative — you describe *how* to do something, step by step. "Create a loop, initialize a counter, check each element, update the result." You control every step.
- Declarative — you describe *what* you want, and let the system figure out how. "Give me all items where price > 100." You specify the result, not the process.
// Imperative: how to filter
const result = [];
for (const item of items) {
if (item.price > 100) result.push(item);
}
// Declarative: what to filter
const result = items.filter(item => item.price > 100);
Both produce the same output. The declarative version is shorter and focuses on the *what*. The imperative version gives you more control but is more verbose. JavaScript supports both styles, and most code uses a mix.
Object-Oriented Programming (OOP)
OOP organizes code around objects — bundles of data and the methods that operate on that data. The core ideas are:
- Encapsulation — objects hide their internal state and expose a controlled interface.
- Inheritance — objects can inherit properties and methods from other objects.
- Polymorphism — different objects can respond to the same message in their own way.
OOP is natural for modeling real-world entities: a User object has a name, an email, and methods like login() and logout(). It shines in GUI applications, game engines, and large systems with many interacting entities.
class User {
constructor(name, email) {
this.name = name;
this.email = email;
}
greet() { return `Hello, I am ${this.name}`; }
}
const ada = new User("Ada", "[email protected]");
ada.greet(); // "Hello, I am Ada"
Functional Programming (FP)
FP organizes code around pure functions — functions that always return the same output for the same input and have no side effects. The core ideas are:
- Pure functions — no mutation, no side effects, deterministic output.
- Immutability — data is never changed; you create new data instead.
- First-class functions — functions are values that can be passed and returned.
FP is natural for data transformation, concurrent processing, and UI rendering. It shines in reactive systems, data pipelines, and anywhere predictability is critical.
// Pure function: same input → same output, no side effects
const double = (n) => n * 2;
const numbers = [1, 2, 3];
const doubled = numbers.map(double); // [2, 4, 6] — original unchanged
JavaScript's Multi-Paradigm Nature
JavaScript is a multi-paradigm language — it supports imperative, OOP, and functional styles. You can write a class-based OOP program, a functional pipeline of pure functions, or a mix of both. This flexibility is powerful but requires discipline: choose a paradigm for each module and be consistent within it.
Choosing a Paradigm
- Use OOP when you have entities with state and behavior (UI components, game objects, domain models).
- Use FP when you transform data through a pipeline (data processing, event handling, calculations).
- Use declarative style by default — it is shorter and less error-prone.
- Drop to imperative when you need fine-grained control or performance.
The best developers are paradigm-fluent: they know all the styles and choose the right one for each situation. You do not need to pick a side — you need to understand the trade-offs.
Example
// --- OOP approach: a shopping cart class ---
class ShoppingCart {
constructor() { this.items = []; }
add(item) { this.items.push(item); }
total() {
return this.items.reduce((sum, i) => sum + i.price, 0);
}
}
const cart = new ShoppingCart();
cart.add({ name: "Book", price: 15 });
cart.add({ name: "Pen", price: 2 });
console.log("OOP total:", cart.total()); // 17
// --- Functional approach: pure functions, immutable data ---
const addItem = (cart, item) => [...cart, item];
const calculateTotal = (cart) =>
cart.reduce((sum, i) => sum + i.price, 0);
let functionalCart = [];
functionalCart = addItem(functionalCart, { name: "Book", price: 15 });
functionalCart = addItem(functionalCart, { name: "Pen", price: 2 });
console.log("FP total:", calculateTotal(functionalCart)); // 17
// --- Declarative vs. Imperative: find expensive items ---
const allItems = [
{ name: "Laptop", price: 999 },
{ name: "Mouse", price: 25 },
{ name: "Keyboard", price: 80 },
];
// Declarative (functional style):
const expensive = allItems.filter(item => item.price > 50);
// Imperative (procedural style):
const expensiveManual = [];
for (const item of allItems) {
if (item.price > 50) expensiveManual.push(item);
}
console.log(expensive); // [{ Laptop }, { Keyboard }]
console.log(expensiveManual); // same result, more code
The OOP version encapsulates cart state in a class. The FP version uses pure functions and immutable arrays (spread creates a new array each time). Both produce the same result. The declarative filter is shorter than the imperative loop.
Try it
- JSON Formatter
Formatting is a pure function: same JSON input always produces the same formatted output, with no side effects. A functional programming example.
- Code Minifier
Minification is a transformation pipeline — a declarative, functional approach to processing code. Input in, smaller output out.
Common mistakes
The mistake
Mixing paradigms inconsistently within the same module.
The fix
Choose a primary paradigm for each module and stick with it. Mixing OOP classes with functional pipelines and imperative loops in one file creates confusion. Consistency within a module is more important than which paradigm you choose.
The mistake
Writing OOP with no encapsulation — everything public, no methods.
The fix
If you use classes, use them for their purpose: encapsulate state and provide methods. If your class is just a bag of public properties, a plain object or function would be simpler. OOP without encapsulation is just structured data.
The mistake
Writing 'functional' code that still mutates shared state.
The fix
Functional programming means pure functions and immutability. If your function modifies an external variable, writes to a file, or changes its input, it is not pure — it is imperative code in a functional costume.
Related Resources
Related Cheatsheets
- JavaScript Cheatsheet
Reference for class syntax, arrow functions, and array methods used across paradigms.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.