JavaScript Callbacks
JavaScript Basics

JavaScript Callbacks

Understanding JavaScript Callbacks

A callback is a function that is passed to another function as an argument and is executed after its parent function has completed. Callbacks are fundamental in JavaScript for handling asynchronous operations and ensuring certain tasks are completed in the correct order.

Basic Concept of Callbacks

In JavaScript, functions can be executed in the order they are called, not necessarily in the order they are defined. This behavior is particularly important in managing the sequence of execution.

Controlling Execution Sequence

You might want to control the execution sequence more strictly, especially when certain operations depend on the results of previous ones.

Direct Function Calls:

function displayMessage(message)
{  

document.getElementById("demo").innerHTML = message;}‍

function calculateSum(num1, num2) 

{  let sum = num1 + num2;  return sum;}‍
let result = calculateSum(5, 5);displayMessage(result);‍

This approach is straightforward but requires calling two functions separately to display the result.

Integrated Function Call:

function displayMessage(message)
{  document.getElementById("demo").innerHTML = message;}‍
function calculateAndDisplay(num1, num2)

{  let sum = num1 + num2;  displayMessage(sum);

}‍

calculateAndDisplay(5, 5);

This method integrates the display within the calculation function, which reduces flexibility as the calculation function is tightly coupled with the display logic.

Using Callbacks for Flexible Control

To combine flexibility with control, you can use a callback function. This allows the calculator function to execute the display function directly after completing its main task.

function displayMessage(message) 
{  
document.getElementById("demo").innerHTML = message;
}‍
function calculateThenCallback(num1, num2, callback) 
{  
let sum = num1 + num2;  callback(sum);

}‍

calculateThenCallback(5, 5, displayMessage);

In this setup, displayMessage is passed as a callback to calculateThenCallback. This approach separates the concerns of calculating and displaying, making the functions reusable and maintainable.

Advanced Callback Usage

Callbacks are not just for simple examples; they shine in handling tasks such as asynchronous operations like data fetching, file operations, or waiting for user input.

function removeNegativeNumbers(numbers, callback) 
{  
const positiveNumbers = numbers.filter(callback);  return positiveNumbers;
}‍
const myNumbers = [4, 1, -20, -7, 5, 9, -6];
const positiveNumbers = removeNegativeNumbers(myNumbers, number => number >= 0);‍

document.getElementById("demo").innerHTML = positiveNumbers;

Here, the callback is used to filter an array, demonstrating how callbacks can be utilized to make functions more dynamic and adaptable to different conditions.

When to Use Callbacks

  • Asynchronous Operations: Callbacks are crucial in asynchronous JavaScript operations, such as network requests, where operations might need to wait for a response before proceeding.
  • Event Handling: They are used extensively in handling user interactions in web pages.
  • Functional Programming: Callbacks enable functional programming techniques, allowing operations like mapping, filtering, and reducing on collections.

Callbacks provide a way to ensure certain code does not execute until other code has finished and are essential for effective JavaScript programming, especially in asynchronous logic flow and events.

Take a look into your desired course