DEV Community

Kurapati Mahesh
Kurapati Mahesh

Posted on

5 3

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

Top 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.

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay