Author -  Sai gowtham

What is a Callback function in JavaScript?

In this tutorial, we are going to learn about callback functions in JavaScript.

What is a Callback function?

When we call a function that function will call other function by itself is called callback function.

example:

function add(a,b,callback){
   return callback(a,b)
}

add(1, 2, function(a,b){
    return a+b;
})

Output:

 3

Did you notice, in the above code we have called the add() function and that function will call the callback function?

The function which accepts the other function as an argument is called a higher-order function.

Where callback functions are commonly used in JavaScript?

  • In Event handling, we commonly used callback functions in JavaScript.

  • Network requests.

Consider we have a button with the click event handler is attached to it. The second argument in the addEventListener method is a callback function.

The callback function only invokes when a click event happens on that button.

<button class="btn" >click</button>

<script>
const btn = document.querySelector('.btn');

btn.addEventListener('click',function(){
   alert('someone hit me');
})

</script>

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY