IIFE and Function Currying
Topic 9JavaScript Part 1
IIFE and Function Currying
Introduction
IIFE(Immediately Invoked Function Expression) and currying are functional programming concepts. An IIFE is a function executed immediately after its declaration. Currying transforms a function with multiple arguments into a series of functions, each taking a single argument.
Key Concepts and Definitions
IIFE
Syntax: (function() { ... })(); Used to avoid polluting the global scope.
Currying
Breaking down a function into smaller functions that return another function. Improves reusability and composition.
Examples
IIFE:
(function() {
let message = 'IIFE executed';
console.log(message);
})();Currying:
function multiply(a) {
return function(b) {
return a * b;
};
}
const double = multiply(2);
console.log(double(5)); // 10Practice Exercises
- •Write an IIFE to initialize variables without affecting the global scope.
- •Create a curried function to calculate discounts on prices.
Summary
IIFE provides immediate execution and scope isolation, while currying enables function composition and partial application.
Interview guide discussion
(0)Join the discussion — login to comment and help other developers.
Login to comment