Functions in JavaScript
Title
Question
Hello Folks,
Can anyone tell me what are higher-order functions in JavaScript, as I started learning javascript, please let me know your suggestions to bridge the gap.
Can anyone tell me what are higher-order functions in JavaScript, as I started learning javascript, please let me know your suggestions to bridge the gap.
Thanks in advance!
JavaScript Functions-in-JS 02-03 min 20-30 sec
Answers:
A Higher-order function is a function that takes a function as an input parameter and returns the modified function.
The name of the function which is taking a function as param is HOF.
// Higher-order function example (function as an argument)
function operateOnNumber(number, operation) {
return operation(number);
}
function square(x) {
return x * x;
}
function double(x) {
return x * 2;
}
console.log(operateOnNumber(5, square)); // Output: 25
console.log(operateOnNumber(3, double)); // Output: 6
<span style="border-radius: 4px; padding: 0px 2px; background-image: linear-gradient(90deg, rgb(211, 227, 253) 50%, rgba(0, 0, 0, 0) 50%); background-position: 0% 0px; background-size: 200% 100%; background-repeat: no-repeat; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(4, 12, 40); animation: 0.75s cubic-bezier(0.05, 0.7, 0.1, 1) 0.25s 1 normal forwards running highlight; font-family: "Google Sans", Arial, sans-serif; font-size: 16px;">Higher order functions are functions that take one or more functions as arguments, or return a function as their result. </span>
<span style="border-radius: 4px; padding: 0px 2px; background-image: linear-gradient(90deg, rgb(211, 227, 253) 50%, rgba(0, 0, 0, 0) 50%); background-position: 0% 0px; background-size: 200% 100%; background-repeat: no-repeat; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(4, 12, 40); animation: 0.75s cubic-bezier(0.05, 0.7, 0.1, 1) 0.25s 1 normal forwards running highlight; font-family: "Google Sans", Arial, sans-serif; font-size: 16px;">
let addData=(a,b)=>{
return(a+b);
}
let finalResult=addData(10,20);
console.log(finalResult); //30
</span>A Higher-order function is a function that takes a function as an input parameter and returns the modified function
let addData=(a,b)=>{
return(a+b);
}
let finalResult=addData(10,20);
console.log(finalResult); //30
</span>
Login to add comment