function createId() {
let id = 0;
return () => ++id;
}
const generateUserId = createId();
const generateProductId = createId();
createId() is a higher-order factory function: 1) It's a function that returns another function, 2) The returned function maintains state via closure, 3) Each call to createId() creates a new, independent counter function, 4) Different generators (generateUserId and generateProductId) maintain separate counters, 5) This pattern is useful for creating stateful functions with independent instances, 6) It combines the factory pattern with higher-order functions to create specialized function instances.