1.

Explain callback functions in JavaScript with the help of an example.

Answer»

A callback function is a function that receives an input from another function and is then called from within the OUTER function to COMPLETE a routine or operation. Here's an illustration of a callback function in JavaScript:

function cityCallout(city) { alert('WELCOME to' + city + '!');}function FOO(callback) { var cityName = prompt('Please input the name of the city here:'); callback(cityName);}foo(cityCallout);

Therefore, if we pass "New York" in the prompt, the output of this piece of code snippet will be "Welcome to New York!".



Discussion

No Comment Found