DEV Community

Nikita Maharana
Nikita Maharana

Posted on

Callback Functions in JavaScript Explained

Callback Functions

A callback function is a function which is passed as an argument to another function.

e.g. function kind(){
console.log("I'm kind");
}
function rude(){
console.log("I'm rude");
}
function fun(a, b){
a();//calls kind()
b();//calls rude()
}
fun(kind, rude);

//Output:
I'm kind
I'm rude

Here, kind and rude are callback functions because they are passed as arguments to another function named as fun.
And fun is called a higher order function because it accepts functions (kind and rude) as parameters.

Top comments (2)

Collapse
 
skeiro profile image
Chirag

Thank you for the article it helps me a lot

Collapse
 
nikita_maharana_879884df2 profile image
Nikita Maharana

Glad it helped 🙌
Thanks for reading the post!