const counter = (function () {
let count = 0;
return {
increment: () => ++count,
get: () => count
};
})();
counter.increment();
console.log(counter.get()); // 1
Topics
37
ArrayBuffer, TypedArrays & Binary Data
Arrays & Array Methods
Async Iterators & Streams
Browser Storage & Web APIs
Classes & Class Syntax
Date, Time & Internationalization (Intl API)
Debugging, Testing & Tooling
Design Patterns in JavaScript
Destructuring, Spread & Rest
DOM & Events
Error Handling
ES Modules
Event Loop & Concurrency
Functional Programming
Iterators & Generators
JSON & Data Serialization
Map, Set, WeakMap & WeakSet
Memory Management & Garbage Collection
Networking: Fetch, XHR, WebSockets & CORS
Numbers, Math & BigInt
Objects, Property Descriptors & Immutability
Optional Chaining & Nullish Coalescing
Package Management, Bundlers & Transpilation (npm, Webpack/Vite, Babel)
Performance Optimization: Debouncing, Throttling & Memoization
Promises & async/await
Prototypes & Inheritance
Proxy & Reflect
Regular Expressions
Scope, Hoisting & Closures
Security: XSS, CSRF & Content Security Policy
Service Workers & Progressive Web Apps
Strings & Template Literals
Symbols & Well-Known Symbols
this & Binding
Types & Coercion
Web Components & Custom Elements
Web Workers & Multithreading
Design Patterns in JavaScript
10 questions found
The Module pattern uses a function closure (or an ES module) to keep internal variables private and expose only a controlled public API, preventing external code from directly manipulating internal state.
Real-world example
Building a small state-tracking utility without leaking its internal variable to the global scope.
Scope
Hoisting & Closures
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.
ES Modules
Maintain a list of subscriber callback functions; provide a subscribe() method to add them and a notify() (or emit()) method that loops through and calls each one when an event occurs — this decouples the event source from its consumers.
class EventEmitter {
#listeners = [];
subscribe(fn) { this.#listeners.push(fn); }
emit(data) { this.#listeners.forEach(fn => fn(data)); }
}
Real-world example
Powering a custom pub-sub system for decoupled communication between UI components.
Classes & Class Syntax
What is the Factory pattern, and how does it differ from calling a class constructor directly?
IntermediateA factory is a function that encapsulates the logic for deciding which class/object to instantiate and how, hiding that complexity from the caller — useful when construction logic is conditional or needs to vary at runtime.
function createShape(type) {
switch (type) {
case 'circle': return new Circle();
case 'square': return new Square();
default: throw new Error('Unknown shape');
}
}
Real-world example
A UI library's createElement() function that returns different component instances based on a type string.
Classes & Class Syntax
By wrapping a function or object with another function that adds behavior before/after calling the original, while preserving its original interface — often done with higher-order functions rather than the class decorator proposal.
function withLogging(fn) {
return (...args) => {
console.log('calling with', args);
return fn(...args);
};
}
const loggedAdd = withLogging((a, b) => a + b);
Real-world example
Wrapping an API call function to automatically add logging or retry behavior without modifying the original function.
Functional Programming
Strategy defines a family of interchangeable algorithms/behaviors as separate functions or objects, and lets you swap which one is used at runtime, replacing sprawling conditional logic with a simple lookup or injected dependency.
const strategies = {
card: (amt) => payWithCard(amt),
paypal: (amt) => payWithPaypal(amt),
};
function pay(method, amount) {
return strategies[method](amount);
}
Real-world example
Supporting multiple payment or shipping calculation methods that can be swapped without touching the core checkout logic.
Design Patterns in JavaScript
JavaScript's native Proxy wraps a target object with traps (get, set, has, etc.) that intercept and can customize fundamental operations on it, letting you add validation, logging, or virtual properties transparently.
const validated = new Proxy({}, {
set(target, prop, value) {
if (prop === 'age' && value < 0) throw new Error('Invalid age');
target[prop] = value;
return true;
}
});
validated.age = -5; // throws
Real-world example
Building a reactive state object (like Vue 3's reactivity system) that tracks property reads and writes.
Proxy & Reflect
Command encapsulates an action (and its parameters) as an object with a consistent execute() method, so actions can be queued, logged, undone, or passed around independently of the code that triggers them.
class AddItemCommand {
constructor(cart, item) { this.cart = cart; this.item = item; }
execute() { this.cart.push(this.item); }
undo() { this.cart.pop(); }
}
Real-world example
Implementing undo/redo functionality in a drawing or text-editing application.
Functional Programming
Adapter wraps an incompatible interface with a new one that matches what your code expects, letting you swap or update the underlying library without rewriting all the code that depends on it.
class LegacyLogger {
logMessage(msg) { console.log('LEGACY:', msg); }
}
class LoggerAdapter {
#legacy = new LegacyLogger();
log(msg) { this.#legacy.logMessage(msg); } // matches modern interface
}
Real-world example
Wrapping an old third-party analytics SDK so the rest of the app can call a consistent, modern log() API.
Classes & Class Syntax
Many GoF patterns exist to work around limitations of statically-typed, class-heavy languages. JavaScript's first-class functions and closures often solve the same problem more simply — e.g., a full class-based Strategy pattern is often overkill when a plain object of functions or even a single higher-order function would do.
// Overkill class-based Strategy:
class AddStrategy { execute(a,b) { return a+b; } }
// Idiomatic JS equivalent:
const add = (a, b) => a + b;
Real-world example
Recognizing when a Java-style pattern is being copy-pasted into JS unnecessarily, adding ceremony without benefit.
Functional Programming