Posts

Showing posts from July, 2026

What is a higher order function

A higher-order function is a function that takes another function as an argument or returns a function . Example 1: Takes a function as an argument function greet ( name ) { return `Hello, ${ name } ` ; } function processUser ( callback ) { console . log( callback ( "John" )); } processUser ( greet ); Output: Hello, John Example 2: Returns a function function multiply ( x ) { return function ( y ) { return x * y ; }; } const double = multiply ( 2 ); console . log( double ( 5 )); // 10 Common Higher-Order Functions in JavaScript map() filter() reduce() forEach() find() setTimeout() Example: const numbers = [ 1 , 2 , 3 ]; const doubled = numbers . map( num => num * 2 ); Here, map() is a higher-order function because it takes a callback function. 

What is a first order function

  A first-order function is a function that doesn’t accept another function as an argument and doesn’t return a function as its return value. i.e, It's a regular function that works with primitive or non-function values. const firstOrder = ( ) => console . log ( "I am a first order function!" ) ;

What is a first class function

 A first-class function means that functions are treated like any other value in JavaScript. They can be: Assigned to a variable Passed as an argument to another function Returned from another function Stored in objects or arrays Example function greet () { return "Hello" ; } // Assigned to a variable const sayHello = greet ; // Passed as an argument function execute ( fn ) { console . log( fn ()); } execute ( greet ); // Hello One-line Interview Answer "In JavaScript, functions are first-class citizens, which means they can be assigned to variables, passed as arguments, returned from other functions, and stored in data structures like any other value."

What are lambda expressions or arrow functions

Arrow functions (also called lambda expressions in some languages) are a shorter way to write functions in JavaScript. They were introduced in ES6 (ECMAScript 2015) . Syntax: const add = ( a , b ) => a + b ; Instead of: function add ( a , b ) { return a + b ; } Key Points for Interviews Shorter and cleaner syntax. Do not have their own this ; they inherit this from the surrounding scope (lexical this ). Best for callbacks ( map , filter , reduce , setTimeout , etc.). Cannot be used as constructors with new . One-line Interview Answer "Arrow functions are a concise way to write functions in JavaScript. They use the => syntax, inherit this from their surrounding scope (lexical this ), and are commonly used for callbacks and cleaner code."