JavaScript Callbacks: Understanding and Using Them

Ruhul Amin
2 min readFeb 7, 2023

--

https://www.freecodecamp.org/news/javascript-callback-function-plain-english/

JavaScript is a powerful and dynamic programming language that allows developers to create complex and interactive web applications. One of the key features of JavaScript is the ability to use callbacks, which are functions that are passed as arguments to other functions. In this article, we’ll explore what callbacks are and how they can be used effectively in JavaScript.

What are Callbacks?

Callbacks are functions that are passed as arguments to other functions and are executed after the main function has completed its work. The term “callback” comes from the fact that the function is called back by the main function once its work is done. Callbacks are a way to handle asynchronous programming in JavaScript, meaning that the function does not block the execution of the rest of the code while it is running. This allows the main function to continue working while the callback is being executed.

How to Use Callbacks in JavaScript

Callbacks can be used in a variety of ways in JavaScript, including as arguments to functions, as event listeners, and as anonymous functions. Let’s look at an example of a callback being used as an argument to a function.

function greeting(name, callback) {
console.log("Hello, " + name + "!");
callback();
}

function goodbye() {
console.log("Goodbye!");
}

greeting("John", goodbye);

// Output:
// Hello, John!
// Goodbye!

In this example, the greeting the function takes two arguments: name and callback. The callback the argument is a reference to the goodbye function, which is then executed inside the greeting function.

Callbacks can also be used as event listeners in JavaScript. For example:

const button = document.querySelector("button");

button.addEventListener("click", function() {
console.log("Button was clicked!");
});

In this example, the addEventListener the method is used to listen for the "click" event on a button element. When the button is clicked, the anonymous function is passed as an argument to addEventListener is executed, logging a message to the console.

Finally, callbacks can also be used as anonymous functions in JavaScript. An anonymous function is a function that is declared without a name and is often used in situations where a function is only needed once. For example:

[1, 2, 3].forEach(function(element) {
console.log(element);
});

// Output:
// 1
// 2
// 3

Conclusion

Callbacks are a crucial part of JavaScript programming, allowing developers to create complex and interactive web applications. By understanding what callbacks are and how they can be used, developers can take advantage of this powerful feature to create dynamic and efficient code. Whether you’re using callbacks as arguments to functions, as event listeners, or as anonymous functions, they can be a valuable tool in your JavaScript development toolkit.

👋 Let’s be friends! connect with me on LinkedIn. Don’t forget to follow me here on Medium, and Fiverr as well.

--

--

Ruhul Amin
Ruhul Amin

Written by Ruhul Amin

Software Engineer,backend developer, Love to write content on programming and technical topics

No responses yet