Single Responsibility Principle in JavaScript - Using Closure Functions (Not Just Classes!)


When developers talk about the Single Responsibility Principle (SRP), the conversation often revolves around classes - “Each class should have only one reason to change.”
But here’s the twist - you don’t need a class to follow SRP. In JavaScript, you can achieve the same principle easily using closures.
In fact, for many small or medium features, closures can make your code simpler, lighter, and just as powerful as classes.
Quick SRP Refresher
The Single Responsibility Principle says:
“A piece of code (class, module, or function) should have only one job.”
Why?
Easier to read and understand.
Easier to maintain when requirements change.
Less chance of breaking unrelated features.
Closures in 2 Lines
A closure in JavaScript happens when an inner function “remembers” variables from its outer function - even after the outer function has finished running.
This means we can store state (data) inside a function without using a class.
Example:
function counter() {
let count = 0; // private state
return {
increment() { count++; },
getCount() { return count; }
};
}
const c1 = counter();
c1.increment();
console.log(c1.getCount()); // 1
See that? We didn’t need a class Counter - closure did the job.
SRP Example: Database Connection Pool
Normally, if we were using a class, it might look like this:
class DBConnectionPool {
constructor() {
this.connections = [];
}
addConnection(conn) {
this.connections.push(conn);
}
getConnections() {
return this.connections;
}
}
But - we can achieve the same SRP using closures:
function createDBConnectionPool() {
const connections = []; // private state
function addConnection(connection) {
connections.push(connection);
}
function getConnections() {
return [...connections]; // return a copy for safety
}
return {
addConnection,
getConnections
};
}
// Usage:
const pool = createDBConnectionPool();
pool.addConnection("DB1");
pool.addConnection("DB2");
console.log(pool.getConnections()); // ["DB1", "DB2"]
Why This Follows SRP
One job: This closure handles only database connection, storage & retrieval.
Private state: The
connections
array is not directly accessible from outside - no one can accidentally mess with it.Encapsulation without classes: Closures give us a way to “hide” data just like class private fields.
When to Choose Closure over Class
Situation | Closure is Great | Class is Better |
Small, focused utilities | ✅ | ❌ |
Need a simple private state | ✅ | ❌ |
Complex entities with inheritance | ❌ | ✅ |
Lightweight, functional style | ✅ | ❌ |
Many instances with shared methods | ❌ | ✅ |
Final Thoughts
The Single Responsibility Principle is not tied to OOP or classes. In JavaScript, functions and closures can be just as good - sometimes even simpler - for creating clean, maintainable code.
So next time you think, “I need a class for this,”
ask yourself - “Could a closure do the job more simply?”
Subscribe to my newsletter
Read articles from SOURAV BERA directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

SOURAV BERA
SOURAV BERA
👋 Hey there! I'm Sourav Bera, a seasoned MERN (MongoDB, Express.js, React.js, Node.js) full-stack developer with over 3 years of hands-on experience in crafting robust and scalable web applications. 💻 I thrive on solving coding challenges and building elegant solutions to complex problems. Whether it's tackling algorithmic puzzles or optimizing performance, I'm always up for a coding adventure! 🌐 Beyond coding, I have a keen interest in Low-Level Design (LLD) and High-Level Design (HLD), where I enjoy architecting systems and crafting elegant solutions that scale. I'm passionate about database design and love crafting efficient data models that power applications seamlessly. 🚀 When I'm not immersed in code, you can find me exploring the latest trends in technology, honing my skills, or sharing insights and experiences with the developer community. Let's connect and embark on a journey of continuous learning and innovation together! https://www.linkedin.com/in/developersouravbera/