Javascript: Higher-order functions

A function can take a function as an argument or can return a function as a value is called an higher-order function.

A function that returns a function

const higherOrderFunc = function() {
return function() {
return 12;
}
}

// w…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Kurapati Mahesh

A function can take a function as an argument or can return a function as a value is called an higher-order function.

  1. A function that returns a function
const higherOrderFunc = function() {
    return function() {
        return 12;
    }
}

// which returns below function hence it is higher order function.
higherOrderFunc(); 
> Ć’ () {
        return 12;
    }

higherOrderFunc()();
> 12
  1. A function that takes a function as an argument
const testFunc = function(x) {
    return x + 12;
}

//which takes function as an argument.
const higherOrderFunc = function(testFunc) {
    return testFunc(8);
}

higherOrderFunc(testFunc);
> 20

Example: 1

function calculate(operation, numbers) {
    return operation(numbers);
}

function addition(numbers) {
    let sum = 0;
    for (const number of numbers) {
        sum+=number;
    }
    return sum;
}

function multiply(numbers) {
    let sum = 1;
    for (const number of numbers) {
        sum*=number;
    }
    return sum;
}

const numbers = [1,2,3,4,5];
console.log(calculate(addition, numbers));
> 15

console.log(calculate(multiply, numbers));
> 120

calculate(multiply, numbers) - Don't append parenthesis while sending function as an argument.

Benefits of higher-order functions:

  1. Reduces code duplication
  2. Single responsibility

In Javascript, functions can take arguments as primitives or objects and return the same called first-order functions.

JS built-in higher order functions are:

arr.reduce(), arr.forEach(), arr.filter(), arr.map()

Thanks.

You can follow me here: https://twitter.com/urstrulyvishwak


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Kurapati Mahesh


Print Share Comment Cite Upload Translate Updates
APA

Kurapati Mahesh | Sciencx (2022-09-30T03:27:47+00:00) Javascript: Higher-order functions. Retrieved from https://www.scien.cx/2022/09/30/javascript-higher-order-functions/

MLA
" » Javascript: Higher-order functions." Kurapati Mahesh | Sciencx - Friday September 30, 2022, https://www.scien.cx/2022/09/30/javascript-higher-order-functions/
HARVARD
Kurapati Mahesh | Sciencx Friday September 30, 2022 » Javascript: Higher-order functions., viewed ,<https://www.scien.cx/2022/09/30/javascript-higher-order-functions/>
VANCOUVER
Kurapati Mahesh | Sciencx - » Javascript: Higher-order functions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/30/javascript-higher-order-functions/
CHICAGO
" » Javascript: Higher-order functions." Kurapati Mahesh | Sciencx - Accessed . https://www.scien.cx/2022/09/30/javascript-higher-order-functions/
IEEE
" » Javascript: Higher-order functions." Kurapati Mahesh | Sciencx [Online]. Available: https://www.scien.cx/2022/09/30/javascript-higher-order-functions/. [Accessed: ]
rf:citation
» Javascript: Higher-order functions | Kurapati Mahesh | Sciencx | https://www.scien.cx/2022/09/30/javascript-higher-order-functions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.