DEV Community

Cover image for what is callback function in javascript
biplavmz
biplavmz

Posted on

1

what is callback function in javascript

  1. a callback function is a function
  2. that is passed as an argument
  3. to another function and is intended to be called later in the execution of that function.

The purpose of a callback function is to

  1. allow for asynchronous programming.
  2. where a function can be executed while other code is running,
  3. and then execute the callback function when it's finished.

Callback functions are commonly used in event handling,
AJAX requests, and other asynchronous operations.

Q. Interview Question related to Callback functions

Q1.Explain what a callback function is and provide a simple example

A callback function is a function that is passed to another function as an argument and is executed after some operation has been completed.

Below is an example of a simple callback function that logs to the console after some operations have been completed.

`function modifyArray(arr, callback) {
// do something to arr here
arr.push(100);
// then execute the callback function that was passed
callback();
}

var arr = [1, 2, 3, 4, 5];

modifyArray(arr, function() {
console.log("array has been modified", arr);
});`

Google JavaScript Technical Interview (Callbacks, Promises, Await/Async)

  1. Callbacks
    What is a Callback in JavaScript
    Callbacks in real Life

  2. Callbacks vs. ES6 Promises & Await / Async
    Why Promises
    What are Promises
    How Promises resolves Callback Hell

Why do we even need Callback in JavaScript?

(https://medium.com/developers-tomorrow/google-javascript-technical-interview-7a20accd6ddf)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post →

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

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

Okay