DEV Community

Kurapati Mahesh
Kurapati Mahesh

Posted on

What is callback in Javascript?

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));
Enter fullscreen mode Exit fullscreen mode

while passing function as argument to another function make sure to ignore appending parenthesis.

Thanks.

Contact: Twitter

Latest comments (1)

Collapse
 
urstrulyvishwak profile image
Kurapati Mahesh

Purposefully, wrote functions instead of arrow functions. It will be more intuitive while reading.

Thank you for suggestion. Its colorful code now.