higher order functions in js mdn code example
Example 1: js higher order functions
function add(number1, number2){
return number1 + number2;
}
function multiply(number1, number2){
return number1 * number2;
}
function calc(number1, number2, fn){
return fn(number1, number2);
}
calc(10, 2, add);
Example 2: js higher order function examples
function copyArrayAndManipulate(array, instructions) {
const output = []
for(let i = 0; i < array.length; i++) {
output.push(instructions(array[i]))
}
return output
}
function multiplyBy10(input) {
return input * 10
}
copyArrayAndManipulate([1, 2, 3], multiplyBy10)
Example 3: higher order function in javascript
let total = 0, count = 1;
while (count <= 10) {
total += count;
count += 1;
}
console.log(total);