IIFEs
An immediately invoked function expression defines a function expression and calls it immediately. Its local bindings belong to that invocation’s scope.
It creates a private working area and returns only what the caller needs.
See the mechanism
a function value is createdFollow one example
The parentheses form an expression; the final () invokes it. prefix is local and does not become an outside binding. config receives the returned object. Modules and blocks often provide clearer isolation in modern code, but this pattern still appears in older scripts and one-time initialization.
Predict, change and check
Predict: If the final () is removed, is config the returned object or the function itself?
Test your explanation: Remove only the call parentheses and inspect typeof config. Restore them, then compare the technique with defining a named setup function and calling it once.
The tasks below ask you to explain that result and apply the idea to different inputs.
Apply and distinguish
A one-time invocation can establish private scope
An IIFE creates a function and invokes it immediately. Its result can be a value, an interface object or a closure that retains private bindings. Parentheses help distinguish an expression from a declaration in common forms; an IIFE is not a new module loader.
Use modules for ordinary file boundaries in a modern module project. An IIFE remains useful for a deliberately isolated initialization experiment. If its result includes a function, trace which outer binding that returned function retains rather than assuming all local state disappears the moment the initial invocation returns.
Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.