JavaScript › Design Patterns in JavaScript

What is the Singleton pattern and how is it implemented in JavaScript?

Beginner

Answer

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 imported
Real-world example A single shared application configuration or logger instance used across many files.

Common follow-ups: What are the downsides of overusing Singletons, especially for testing?