What is a callback function in JavaScript?

What is a callback function in JavaScript?

a) A function passed as an argument to another function
b) A function that returns multiple values
c) A function that runs in the background
d) A function that loops indefinitely

Answer:

a) A function passed as an argument to another function

Explanation:

A callback function is a function passed as an argument to another function and is invoked after the completion of that function. Callbacks are commonly used for asynchronous programming in JavaScript.

Example:


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

function processUserInput(callback) {
    let name = "John";
    callback(name);
}

processUserInput(greeting); // Output: "Hello John"
    

Reference:

JavaScript MCQ (Multiple-Choice Questions)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top