Dependency injection (DI) is a pattern where an object receives its dependencies from outside (injected) rather than creating them itself. It promotes loose coupling, testability, and flexibility — a fundamental, widely-used pattern in modern software.
What dependency injection is
WITHOUT DI → a class CREATES its own dependencies (tightly coupled):
class OrderService { constructor() { this.db = new Database(); } } // hardcoded dependency
WITH DI → dependencies are PROVIDED (injected) from outside:
class OrderService { constructor(db) { this.db = db; } } // db is injected
→ the object doesn't create/control its dependencies → they're given to it
