higherorder functions js code example

Example: js higher order functions

// function that adds 2 numbers
function add(number1, number2){
	return number1 + number2;
}

// function that multiplies 2 numbers
function multiply(number1, number2){
	return number1 * number2;
}

// higher order function: takes 2 arguments and a function in this case
function calc(number1, number2, fn){
	return fn(number1, number2);
}

// this is how you would use it
calc(10, 2, add); // you can also use 'multiply' or any other function

// output: 12