use of higher-order functions 

use of higher-order functions 

Make use of higher-order functions like map, filter, and reduce to process arrays:

const numbers = [1, 2, 3, 4, 5];

// Use map

const double = => number * 2);

// Use filter

const even = numbers.filter(number => number % 2 === 0);

// Use reduce

const sum = numbers.reduce((acc, number) => acc + number, 0);

A higher-order function is a function that takes one or more functions as arguments, or returns a function as its result. Higher-order functions are a fundamental concept in functional programming and are widely used in JavaScript for a variety of purposes.

Here’s an example of a higher-order function that takes a function as an argument:

function repeat(operation, num) {

  for (let i = 0; i < num; i++) {




function hello() {



repeat(hello, 5);

// Output:

// Hello

// Hello

// Hello

// Hello

// Hello

In this example, the repeat function takes two arguments: operation and num. The operation argument is a function that is executed num times by the repeat function. In this case, the hello function is passed as the operation argument, and the repeat function is executed five times, which results in the hello function being called five times and the string “Hello” being logged to the console five times.

Here’s another example of a higher-order function that returns a function as its result:

function greaterThan(n) {

  return function(m) {

    return m > n;



let greaterThan10 = greaterThan(10);

console.log(greaterThan10(9));  // false

console.log(greaterThan10(11));  // true

In this example, the greaterThan function takes a single argument n and returns a new function that takes a single argument m and returns true if m is greater than n, and false otherwise. The greaterThan10 variable is assigned the result of calling greaterThan(10), which returns a function that checks if its argument is greater than 10. The greaterThan10 function is then used to check if 9 and 11 are greater than 10, and the results are logged to the console.

Higher-order functions are widely used in JavaScript for tasks such as processing arrays, creating new functions with specific behavior, and composing functions. They allow you to write code that is more modular, flexible, and reusable, and they can greatly simplify complex tasks by breaking them down into smaller, more manageable components.

In conclusion, higher-order functions are a powerful concept in functional programming and are widely used in JavaScript to simplify complex tasks and make code more modular, flexible, and reusable. By taking functions as arguments or returning functions as results, higher-order functions provide a powerful tool for abstracting and composing functionality in your code.

const nums = [1,2,3,4,5,6,7,8];


const dou = => val *2);


const even = nums.filter(val => val % 2 ===0);


const odd = nums.filter(val => val % 2 !== 0);


const sum = nums.reduce((acc,val)=> acc + val,0);


function repeat(oper,num){

for(let i=0;i<num;i++){




function hello(val,total){

console.log(`${val+1} of ${total} Hello`);



function greater(a){

return function(b){

return b > a;



let numVal = 7;

let greater10 = greater(numVal);

for(let i=0;i<10;i++){

console.log(`${5+i} is Greater than ${numVal} ${greater10(5+i)}`);


WP Twitter Auto Publish Powered By :