JavaScript › Design Patterns in JavaScript
What is the Singleton pattern and how is it implemented in JavaScript?
BeginnerAnswer
Singleton ensures only one instance of an object exists across the whole application, typically by exporting a single already-created instance from a module — since ES modules are cached, importing it anywhere returns the same instance.
// config.js
class Config {
constructor() { this.settings = {}; }
}
export default new Config(); // same instance everywhere it's importedReal-world example A single shared application configuration or logger instance used across many files.