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 (2)
Just a heads up that you can add highlighting to the code blocks if you'd like. Just change:
... to specify the language:
More details in our editor guide!
About the actual post, don't forget about arrow functions, that would make your code way easier to read:
Cheers!
Purposefully, wrote functions instead of arrow functions. It will be more intuitive while reading.
Thank you for suggestion. Its colorful code now.