Side by side
OOPvsFunctional Programming
What is the difference between OOP and functional programming?
Updated 2 min read7 differences
In short
OOP bundles data and the methods that change it into objects, while functional programming transforms immutable data with pure functions.
OOP
Object-Oriented Programming
OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
Read the page on OOPFunctional Programming
Functional programming is a style of building software from pure functions that avoid changing shared data, making code more predictable and easier to test.
Read the page on Functional ProgrammingOOP and Functional Programming compared
| Aspect | OOP | Functional Programming |
|---|---|---|
| Core unit | Objects that combine data and behavior | Functions that turn inputs into outputs |
| State | Mutable state kept inside objects | Immutable data; new values instead of changes |
| Side effects | Common and expected inside methods | Avoided or pushed to the edges of the program |
| Code reuse | Inheritance, interfaces and object composition | Higher-order functions and function composition |
| Concurrency | Shared mutable state needs locks or careful design | Immutable data is naturally safe to share across threads |
| Testing | Often needs object setup and mocks | Pure functions are tested with inputs and expected outputs |
| Typical languages | Java, C#, C++, Python and Ruby | Haskell, Elixir, Clojure, F# and FP-style JavaScript |
The difference, explained
Object-oriented programming (OOP) models a program as objects, such as a User or an Order, each holding its own state and exposing methods that act on it. Functional programming (FP) models a program as a pipeline of functions that take inputs and return outputs, ideally without changing anything outside themselves.
The core difference is how each handles state. OOP keeps state inside objects and changes it through methods, using encapsulation to control who can touch it. FP avoids shared mutable state: instead of modifying data, functions return new values, which makes code easier to test and safer to run in parallel.
Most modern languages support both, and good code often mixes them. You might use classes to model the main concepts of a domain and write their logic with small pure functions, map, filter and immutable data. JavaScript, Python, Kotlin, Scala and C# all make this combination natural.
A common misconception is that FP means no objects, or that OOP means everything must be a class. They are styles, not strict rules: the real question is where state lives and how it changes, and each style offers tools suited to different problems.
Which one should you use?
Choose OOP when…
- Your domain has clear entities with their own state and behavior.
- You model long-lived things like UI widgets, game objects or accounts.
- Your team and framework are built around classes.
Choose Functional Programming when…
- You transform data through a series of steps.
- Code runs concurrently and shared state is a risk.
- You want small, predictable functions that are easy to test.
A shopping cart in each style
// OOP: the object holds state and changes it
class Cart {
constructor() {
this.items = [];
}
add(item) {
this.items.push(item);
}
total() {
return this.items.reduce((sum, i) => sum + i.price, 0);
}
}// FP: pure functions return new data
const add = (items, item) => [...items, item];
const total = (items) =>
items.reduce((sum, i) => sum + i.price, 0);
let cart = [];
cart = add(cart, { name: "Book", price: 12 });
console.log(total(cart)); // 12Readers ask
Is functional programming better than OOP?
Neither is better in general. FP shines for data transformations and concurrent code, while OOP fits well when modeling entities with state and behavior; many codebases use both.
Can you mix OOP and functional programming?
Yes. Most mainstream languages support both styles, and a common approach is to use objects for structure and pure functions for logic.
Is JavaScript object-oriented or functional?
Both. JavaScript has classes and prototypes for OOP, and first-class functions, closures and array methods like map and filter for a functional style.