Functions and Higher-Order Functions
Use declarations, expressions, arrows, callbacks, and higher-order functions correctly.
Lesson content
🎯 Goal By the end of this lesson, you'll understand: What is a Function? Why do we use Functions? What is a Higher-Order Function? Real-world examples Interview questions What is a Function? A function is a reusable block of code that performs a specific task. Instead of writing the same code again and again, write it once and call it whenever you need it. Example function greet () { console . log( "Hello, Ajay!" ); } greet (); Output Hello, Ajay! Why Use Functions? ❌ Without Function console . log( "Hello!" ); console . log( "Hello!" ); console . log( "Hello!" ); ✅ With Function function sayHello () { console . log( "Hello!" ); } sayHello (); sayHello (); sayHello (); Benefit: Less code, easier to maintain. Function with Parameters Parameters let you pass values into a function. function greet ( name ) { console . log( "Hello " + name ); } greet ( "Ajay" ); greet ( "Rahul" ); Output Hello Ajay Hello Rahul Function with Return Value A function can return a result. function add ( a , b ) { return a + b ; } const result = add ( 10 , 20 ); console . log( result ); Output 30 Real-Life Example 🧮 Imagine a calculator. Instead of calculating manually every time, Create one function. function multiply ( a , b ) { return a * b ; } console . log( multiply ( 5 , 4 )); console . log( multiply ( 6 , 3 )); Output 20 18 What is a Higher-Order Function? A Higher-Order Function (HOF) is a function that: ✅ Accepts another function as an argument, or ✅ Returns another function. Simply put: A function that works with other functions is called a Higher-Order Function. Example 1: Function as a Parameter function greet ( name ) { console . log( "Hello " + name ); } function processUser ( callback ) { callback ( "Ajay" ); } processUser ( greet ); Output Hello Ajay Explanation greet is passed as an argument. processUser calls it later. Real-Life Example 📦 Imagine ordering food online. You place an order. The restaurant prepares it. When it's ready, they call you. The restaurant doesn't know what you'll do next . It simply calls your callback. This is exactly how Higher-Order Functions work. Example 2: Returning a Function function multiplyBy ( num ) { return function ( value ) { return value * num ; }; } const double = multiplyBy ( 2 ); console . log( double ( 5 )); Output 10 Explanation multiplyBy(2) │ ▼ Returns a new function │ ▼ double(5) │ ▼ 10 Most Common Higher-Order Functions 1. map() Transforms every item in an array. const numbers = [ 1 , 2 , 3 ]; const result = numbers . map( function ( num ) { return num * 2 ; }); console . log( result ); Output [2, 4, 6] 2. filter() Keeps only matching items. const numbers = [ 1 , 2 , 3 , 4 , 5 ]; const even = numbers . filter( function ( num ) { return num % 2 === 0 ; }); console . log( even ); Output [2, 4] 3. forEach() Runs a function for every item. const fruits = [ "Apple" , "Banana" , "Mango" ]; fruits . forEach( function ( fruit ) { console . log( fruit ); }); Output Apple Banana Mango Why Higher-Order Functions? Without HOF console . log( "Processing Ajay" ); console . log( "Processing Rahul" ); console . log( "Processing Aman" ); With HOF const users = [ "Ajay" , "Rahul" , "Aman" ]; users . forEach( function ( user ) { console . log( "Processing " + user ); }); Benefits: ✅ Less code ✅ Cleaner code ✅ Easier to reuse ✅ Easier to maintain Difference Function Higher-Order Function Performs a task Works with other functions Can accept data Can accept or return functions Example: add() Example: map() , filter() , forEach() Interview Question What is the output? function sayHi ( name ) { return "Hi " + name ; } function execute ( fn ) { console . log( fn ( "Ajay" )); } execute ( sayHi ); Output Hi Ajay Key Points ✅ A Function is a reusable block of code. ✅ Functions can accept parameters and return values . ✅ A Higher-Order Function accepts another function or returns one. ✅ Common Higher-Order Functions include map() , filter() , reduce() , and forEach() . Easy Formula 📦 Function = Does a task. 🔄 Higher-Order Function = Uses or returns another function.