Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 5 of 10 · Functions and Arrow Functions
Interview question

What is a higher-order function? Higher-order function क्या है?

Answer

A higher-order function is a function that either takes one or more functions as arguments, or returns a function. This is a fundamental concept in functional programming.

// Higher-order function that takes a function as argument
function map(arr, fn) {
    const result = [];
    for (let i = 0; i < arr.length; i++) {
        result.push(fn(arr[i]));
    }
    return result;
}

const nums = [1, 2, 3, 4];
const doubled = map(nums, x => x * 2);
console.log(doubled); // [2, 4, 6, 8]

// Higher-order function that returns a function
function multiplyBy(factor) {
    return function(number) {
        return number * factor;
    };
}

const double = multiplyBy(2);
const triple = multiplyBy(3);

console.log(double(5)); // 10
console.log(triple(5)); // 15

// Real-world examples from JavaScript
const users = [
    { name: 'John', age: 30 },
    { name: 'Jane', age: 25 },
    { name: 'Bob', age: 35 }
];

// Array.prototype.filter is a higher-order function
const over30 = users.filter(user => user.age > 30);

// Array.prototype.map is a higher-order function
const names = users.map(user => user.name);

// Array.prototype.reduce is a higher-order function
const totalAge = users.reduce((sum, user) => sum + user.age, 0);

// Composing higher-order functions
function compose(f, g) {
    return x => f(g(x));
}

const add5 = x => x + 5;
const multiply2 = x => x * 2;
const add5ThenMultiply2 = compose(multiply2, add5);

console.log(add5ThenMultiply2(3)); // (3 + 5) * 2 = 16

Interview tip: Emphasize that higher-order functions enable functional programming patterns like map, filter, reduce. They're also key to decorators, middleware, and partial application. Mention that JavaScript's first-class functions make this possible.

Higher-order function एक ऐसा function है जो दूसरे functions को arguments लेता है या function return करता है। Functional programming का fundamental concept है।

// Function को argument लेता है
function map(arr, fn) {
    const result = [];
    for (let i = 0; i < arr.length; i++) {
        result.push(fn(arr[i]));
    }
    return result;
}

const doubled = map([1,2,3], x => x * 2);
// [2, 4, 6]

// Function return करता है
function multiplyBy(factor) {
    return function(number) {
        return number * factor;
    };
}

const double = multiplyBy(2);
console.log(double(5)); // 10

// Real-world examples
const users = [
    { name: 'John', age: 30 },
    { name: 'Jane', age: 25 }
];

const over30 = users.filter(user => user.age > 30);
const names = users.map(user => user.name);
const totalAge = users.reduce((sum, user) => sum + user.age, 0);

इंटरव्यू टिप: Emphasize करें higher-order functions functional programming patterns जैसे map, filter, reduce को enable करते हैं। Decorators, middleware, और partial application के लिए भी key हैं।

Was this answer clear?