Today you will learn:
By the end, you will write cleaner, reusable, and more powerful functions.
A function that takes another function as argument or returns a function.
const add = x => y => x + y;
console.log(add(2)(3)); // 5
Immediately Invoked Function Expression runs immediately:
(function(){
console.log("IIFE runs immediately!");
})();
Transform a function with multiple arguments into a sequence of functions:
const multiply = x => y => x * y;
console.log(multiply(2)(3)); // 6
Combine multiple functions to produce a new function:
const compose = (f, g) => x => f(g(x));
const double = x => x * 2;
const square = x => x * x;
console.log(compose(square, double)(3)); // 36
const add = x => y => x + y;
console.log(add(2)(3));
(function(){
console.log("IIFE runs immediately!");
})();
Complete the following:
Example:
// Curried multiplication function
const multiply = x => y => x * y;
console.log(multiply(4)(5)); // 20
// IIFE printing name
(function(){
console.log("Alex");
})();