← Back to JavaScript Course | Chapter 6: Functions Advanced | Lesson 5 of 8

JS के IIFEs

एक IIFE एक ऐसा function है जो बनते ही चल जाता है, जैसे एक firework जो जलाते ही फूट जाता है। यह अपने variables को बाकी code से private रखता है।
Syntax
javascript
(function() {
  // runs immediately
})();

(() => {
  // runs immediately
})();

IIFE क्या है?

IIFE का मतलब है Immediately Invoked Function Expression। यह बनते ही चल जाता है। (function(){ ... })() में code को लपेटना इसे तुरंत एक बार execute करता है, बाद में कोई named function बचाए बिना।

उदाहरण: What Is an IIFE?

javascript
(function () {
  // Print "Runs immediately" to the console
  // Print "Runs immediately" to the console
  console.log("Runs immediately");
})();

IIFEs क्यों इस्तेमाल करें?

एक IIFE अपना खुद का scope बनाता है। इसके अंदर के variables global variables नहीं बनते। ES modules के standard बनने से पहले, यह libraries के लिए अपने internal helper variables से global namespace को गंदा होने से बचाने का मुख्य तरीका था।

उदाहरण: Why Use IIFEs?

javascript
(function () {
  var secret = "hidden"; // not a global variable
  console.log(secret);
})();
console.log(typeof secret); // "undefined"

Return के साथ IIFE

एक IIFE तुरंत एक value return कर सकता है। उस returned value को एक variable में store करें। यह pattern, const result = (function(){ ... return value; })(), आपको local helper variables का इस्तेमाल करके एक value calculate करने देता है जो IIFE के बाहर leak नहीं होते।

उदाहरण: IIFE With Return

javascript
const result = (function () {
  // Declare the constant `a`, set to `5, b = 10`
  // Declare the constant `a`, set to `5, b = 10`
  const a = 5, b = 10;
  // Return `a + b` from this function
  // Return `a + b` from this function
  return a + b;
})();
// Print `result` to the console
// Print `result` to the console
console.log(result);

Parameters के साथ IIFE

एक IIFE एक normal function की तरह ही arguments स्वीकार कर सकता है। उदाहरण के लिए, (function(name){ console.log(name); })(Alice) एक regular function को कॉल करने जैसा व्यवहार करता है, बस definition के समय ठीक एक बार invoke होते हुए।

उदाहरण: IIFE With Parameters

javascript
(function (name) {
  // Print `name` to the console
  // Print `name` to the console
  console.log(name);
})("Alice");

Modern इस्तेमाल

आज modules ज़्यादा आम हैं, पर IIFEs अभी भी उपयोगी हैं जब आपको किसी script में एक isolated scope चाहिए।

module-based codebases में भी, एक छोटा IIFE कभी-कभी इस्तेमाल किया जाता है जब आपको setup logic का एक self-contained block चाहिए जिसका internal हिस्सा दिखना नहीं चाहिए।

उदाहरण: Modern Use

javascript
const config = (function () {
  // Declare the constant `secret`, set to "key123"
  // Declare the constant `secret`, set to "key123"
  const secret = "key123";
  // Return `{ getKey: () => secret }` from this function
  // Return `{ getKey: () => secret }` from this function
  return { getKey: () => secret };
})();
// Print `config.getKey()` to the console
// Print `config.getKey()` to the console
console.log(config.getKey());
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. wrapping parentheses भूल जाना, जैसे function() {}(), जो एक SyntaxError है।
  2. trailing () भूल जाना, इसलिए function define तो होता है लेकिन कभी नहीं चलता।
  3. किसी IIFE से पहले वाली line पर semicolon छोड़ देना, इसलिए JavaScript दोनों lines को जोड़ देता है और एक error throw करता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.