Immediately invoked function expression
An immediately invoked function expression (or IIFE, pronounced "iffy" ) is a programming language idiom which produces a lexical scope using function scoping.[1] It was popular in JavaScript as a method of supporting modular programming before the introduction of more standardized solutions such as CommonJS and ES modules.[3] Immediately invoked function expressions can be used to avoid variable hoisting from within blocks, protecting against polluting the global environment and simultaneously allowing public access to methods while retaining privacy for variables defined within the function.[1] In other words, it wraps functions and variables, keeping them out of the global scope and giving them a local scope.[src]
An immediately invoked function expression (or IIFE, pronounced "iffy" ) is a programming language idiom which produces a lexical scope using function scoping.[1] It was popular in JavaScript as a method of supporting modular programming before the introduction of more standardized solutions such as CommonJS and ES modules.[3] Immediately invoked function expressions can be used to avoid variable hoisting from within blocks, protecting against polluting the global environment and simultaneously allowing public access to methods while retaining privacy for variables defined within the function.[1] In other words, it wraps functions and variables, keeping them out of the global scope and giving them a local scope.[src] (function () { /* ...[src] */ })(); (function () { /* ...[src] */ }()); (() => { /* ...[src] */ })(); // With ES6 arrow functions (though parentheses only allowed on outside) In contexts where an expression is expected, wrapping in parentheses is not necessary: let f = function () { /* ...[8] */ }(); true && function () { /* ...[src] */ }(); 0, function () { /* ...[src] */ }(); Passing variables into the scope is done as follows: (function(a, b) { /* ...[src] */ })("hello", "world"); (function(a="hello", b="world") { /* ...[src] */ })(); // also works An initial parenthesis is one case where the automatic semicolon insertion (ASI) in JavaScript can cause problems; the expression is instead interpreted as a call to the last term on the preceding line.[6] In some styles that omit optional semicolons, the semicolon is placed in front of the parenthesis, and is known as a defensive semicolon.[7] For example: a = b + c (function () { // code })(); ...to avoid being parsed as c() .[src] ==Examples== The key to understanding design patterns such as IIFE is to realize that prior to ES6, JavaScript only featured function scope (thus lacking block scope), passing values by reference inside closures.[3] This is no longer the case, as the ES6 version of JavaScript implements block scoping using the new let and const keywords.[8] // Before ES6: Creating a scope using an IIFE var foo = 1; var bar = 2; (function(){ var foo = 3; // shadows the outer `foo` bar = 4; // overwrites the outer `bar` })(); console.log(foo, bar); // 1 4 // Since ES6: Creating a scope using curly brackets in combination with let and const const foo = 1; let bar = 2; { const foo = 3; // shadows the outer `foo` bar = 4; // overwrites the outer `bar` } console.log(foo, bar); // 1 4 let v, getValue; v = 1; getValue = function () { return v; }; v = 2; getValue(); // 2 While the result may seem obvious when updating v manually, it can produce unintended results when getValue() is defined inside a loop.[8] Hereafter the function passes v as an argument and is invoked immediately, preserving the inner function's execution context.[1] let v, getValue; v = 1; getValue = (function (x) { return function () { return x; }; })(v); v = 2; getValue(); // 1 This is equivalent to the following code: let v, getValue; v = 1; function f(x) { return function () { return x; }; }; getValue = f(v); v = 2; getValue(); // 1 // "counter" is a function that returns an object with properties, which in this case are functions.[src] let counter = (function () { let i = 0; return { get: function () { return i; }, set: function (val) { i = val; }, increment: function () { return ++i; } }; })(); // These calls access the function properties returned by "counter".[src] counter.get(); // 0 counter.set(3); counter.increment(); // 4 counter.increment(); // 5 If we attempt to access counter.i from the global environment, it will be undefined, as it is enclosed within the invoked function and is not a property of counter .[1] Likewise, if we attempt to access i , it will result in an error, as we have not declared i in the global environment.[src]
27 sentences · 27 cited inline · 10 references stored
References
- Alman, Ben. Immediately Invoked Function Expressions . 15 November 2010. · retrieved 2026-08-18 · rev 1370187229
Cite this reference
MLA "Alman, Ben. Immediately Invoked Function Expressions . 15 November 2010." Alman, Ben. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229. Accessed 28 November 2026.
Chicago "Alman, Ben. Immediately Invoked Function Expressions . 15 November 2010." Alman, Ben. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229.
APA Alman, Ben. (2026). Alman, Ben. Immediately Invoked Function Expressions . 15 November 2010. Retrieved November 28, 2026, from https://en.wikipedia.org/w/index.php?oldid=1370187229
- Strict mode . Mozilla JavaScript Ref . · retrieved 2026-08-18 · rev 1370187229
Cite this reference
MLA "Strict mode . Mozilla JavaScript Ref ." Strict mode. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229. Accessed 28 November 2026.
Chicago "Strict mode . Mozilla JavaScript Ref ." Strict mode. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229.
APA Strict mode. (2026). Strict mode . Mozilla JavaScript Ref . Retrieved November 28, 2026, from https://en.wikipedia.org/w/index.php?oldid=1370187229
- McGinnis, Tyler. JavaScript Modules: From IIFEs to CommonJS to ES6 Modules . ui.dev . · retrieved 2026-08-18 · rev 1370187229
Cite this reference
MLA "McGinnis, Tyler. JavaScript Modules: From IIFEs to CommonJS to ES6 Modules . ui.dev ." McGinnis, Tyler. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229. Accessed 28 November 2026.
Chicago "McGinnis, Tyler. JavaScript Modules: From IIFEs to CommonJS to ES6 Modules . ui.dev ." McGinnis, Tyler. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229.
APA McGinnis, Tyler. (2026). McGinnis, Tyler. JavaScript Modules: From IIFEs to CommonJS to ES6 Modules . ui.dev . Retrieved November 28, 2026, from https://en.wikipedia.org/w/index.php?oldid=1370187229
- Grouping operator . Mozilla Developer Network . 2 October 2023. · retrieved 2026-08-18 · rev 1370187229
Cite this reference
MLA "Grouping operator . Mozilla Developer Network . 2 October 2023." Grouping operator. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229. Accessed 28 November 2026.
Chicago "Grouping operator . Mozilla Developer Network . 2 October 2023." Grouping operator. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229.
APA Grouping operator. (2026). Grouping operator . Mozilla Developer Network . 2 October 2023. Retrieved November 28, 2026, from https://en.wikipedia.org/w/index.php?oldid=1370187229
- Axel Rauschmayer. ExploringJS . · retrieved 2026-08-18 · rev 1370187229
Cite this reference
MLA "Axel Rauschmayer. ExploringJS ." Axel Rauschmayer. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229. Accessed 28 November 2026.
Chicago "Axel Rauschmayer. ExploringJS ." Axel Rauschmayer. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229.
APA Axel Rauschmayer. (2026). Axel Rauschmayer. ExploringJS . Retrieved November 28, 2026, from https://en.wikipedia.org/w/index.php?oldid=1370187229
- JavaScript Semicolon Insertion: Everything you need to know . 28 May 2010. · retrieved 2026-08-18 · rev 1370187229
Cite this reference
MLA "JavaScript Semicolon Insertion: Everything you need to know . 28 May 2010." JavaScript Semicolon Insertion: Everything you need to know. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229. Accessed 28 November 2026.
Chicago "JavaScript Semicolon Insertion: Everything you need to know . 28 May 2010." JavaScript Semicolon Insertion: Everything you need to know. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229.
APA JavaScript Semicolon Insertion: Everything you need to know. (2026). JavaScript Semicolon Insertion: Everything you need to know . 28 May 2010. Retrieved November 28, 2026, from https://en.wikipedia.org/w/index.php?oldid=1370187229
- Marohnić, Mislav. Semicolons in JavaScript are optional . 7 May 2010. · retrieved 2026-08-18 · rev 1370187229
Cite this reference
MLA "Marohnić, Mislav. Semicolons in JavaScript are optional . 7 May 2010." Marohnić, Mislav. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229. Accessed 28 November 2026.
Chicago "Marohnić, Mislav. Semicolons in JavaScript are optional . 7 May 2010." Marohnić, Mislav. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229.
APA Marohnić, Mislav. (2026). Marohnić, Mislav. Semicolons in JavaScript are optional . 7 May 2010. Retrieved November 28, 2026, from https://en.wikipedia.org/w/index.php?oldid=1370187229
- Orendorff, Jason. ES6 In Depth: let and const . Mozilla Hacks – the Web developer blog . · retrieved 2026-08-18 · rev 1370187229
Cite this reference
MLA "Orendorff, Jason. ES6 In Depth: let and const . Mozilla Hacks – the Web developer blog ." Orendorff, Jason. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229. Accessed 28 November 2026.
Chicago "Orendorff, Jason. ES6 In Depth: let and const . Mozilla Hacks – the Web developer blog ." Orendorff, Jason. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229.
APA Orendorff, Jason. (2026). Orendorff, Jason. ES6 In Depth: let and const . Mozilla Hacks – the Web developer blog . Retrieved November 28, 2026, from https://en.wikipedia.org/w/index.php?oldid=1370187229
- Alman, Ben. simple-iife-example.js . Github . retrieved 5 February 2013 · retrieved 2026-08-18 · rev 1370187229
Cite this reference
MLA "Alman, Ben. simple-iife-example.js . Github . retrieved 5 February 2013." Alman, Ben. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229. Accessed 28 November 2026.
Chicago "Alman, Ben. simple-iife-example.js . Github . retrieved 5 February 2013." Alman, Ben. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229.
APA Alman, Ben. (2026). Alman, Ben. simple-iife-example.js . Github . retrieved 5 February 2013. Retrieved November 28, 2026, from https://en.wikipedia.org/w/index.php?oldid=1370187229
- Baagoe, Johannes. Closing parenthesis in function's definition followed by its call . · retrieved 2026-08-18 · rev 1370187229
Cite this reference
MLA "Baagoe, Johannes. Closing parenthesis in function's definition followed by its call ." Baagoe, Johannes. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229. Accessed 28 November 2026.
Chicago "Baagoe, Johannes. Closing parenthesis in function's definition followed by its call ." Baagoe, Johannes. 2026-08-18. https://en.wikipedia.org/w/index.php?oldid=1370187229.
APA Baagoe, Johannes. (2026). Baagoe, Johannes. Closing parenthesis in function's definition followed by its call . Retrieved November 28, 2026, from https://en.wikipedia.org/w/index.php?oldid=1370187229
Cite this article
MLA
"Immediately invoked function expression." encyclopegia.org, 2026, https://encyclopegia.org/wiki/immediately-invoked-function-expression. Accessed 28 November 2026.
Chicago
"Immediately invoked function expression." encyclopegia.org. Accessed November 28, 2026. https://encyclopegia.org/wiki/immediately-invoked-function-expression.
APA
encyclopegia.org. (2026). Immediately invoked function expression. Retrieved November 28, 2026, from https://encyclopegia.org/wiki/immediately-invoked-function-expression
Accessed November 28, 2026 · CC BY-SA 4.0 · citation page · JSON