CKTIS / JavaScript
IIFE — immediately invoked function expression
An IIFE creates a function and calls it immediately. It runs code in its own function scope, accepts arguments, and returns a result without a separate function declaration.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
;(function (parameter) { /* body */ })(argument);IIFE stands for Immediately Invoked Function Expression: a function expression called directly after the function is created. It is a syntax pattern, not a keyword. “Immediately” means when execution reaches that expression.
In (function () {})(), the parentheses around function () {} establish an expression context rather than a declaration. The final () calls the function; without it, you obtain the function itself. The function can have a name, so an IIFE need not be anonymous.
Parameters and variables declared inside the function, including those declared with var, stay in its scope. This is not a security boundary: the function can access available outer variables. return determines the call's result; without a returned value, the result is undefined.
When a standalone IIFE starts with ( after another expression, a leading ; prevents the two statements from accidentally joining. The example uses classic function syntax without arrow functions. The IIFE itself needs no host API, but the example prints the result using the host-provided console.log. In a browser, this is part of a Web API; in Node.js, it is a method of Node.js's console.
Examples
var result = (function (start) {
var offset = 2;
return start + offset;
})(40);
console.log(result);Result
42
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.