Callback: A function passed as an argument to other function is called callback function.
function calculation(a, b, fn) {
return fn(a, b);
}
function add(a, b) {
return a + b;
}
function mul(a, b) {
return a * b;
}
function sub(a, b) {
return a - b;
}
function div(a, b) {
return a / b;
}
console.log(calculation(4, 2, mul));
while passing function as argument to another function make sure to ignore appending parenthesis.
Thanks.
Contact: Twitter
Top comments (1)
Purposefully, wrote functions instead of arrow functions. It will be more intuitive while reading.
Thank you for suggestion. Its colorful code now.