A callback is a function that is passed as an argument to another function and is called inside it at a certain moment. Callback allows one function to “call” another function when some operation is completed.
// Regular function
function greet(name) {
console.log('Hello, ' + name);
}
// Function accepting callback
function processUser(name, callback) {
// Do something with name
const processedName = name.toUpperCase();
// Call callback
callback(processedName);
}
// Usage
processUser('john', greet); // 'Hello, JOHN'A callback is like asking someone to pass a message to another person. You give your friend a note (function) and say: “When you meet Peter, give him this!”. The callback is that note! 📝
A callback is a function that you pass to another function so that it calls it at the right moment. This allows functions to work together and coordinate their actions.
// Callback function
function showMessage(message) {
console.log('Message: ' + message);
}
// Function using callback
function doSomething(action, callback) {
console.log('Doing ' + action);
callback('Done!');
}
// Usage
doSomething('work', showMessage);
// 'Doing work'
// 'Message: Done!'function calculate(a, b, operation, callback) {
const result = operation(a, b);
callback(result);
}
function showResult(result) {
console.log('Result: ' + result);
}
// Usage
calculate(5, 3, (x, y) => x + y, showResult); // 'Result: 8'
calculate(5, 3, (x, y) => x * y, showResult); // 'Result: 15'// Simulating data loading
function loadData(callback) {
console.log('Starting loading...');
// Simulating delay
setTimeout(() => {
const data = 'Data loaded';
callback(data);
}, 1000);
}
// Usage
loadData(function(result) {
console.log('Success: ' + result);
});// Simulating click handler
function onClick(callback) {
console.log('Waiting for click...');
// When click happens
callback('Click performed!');
}
// Usage
onClick(function(message) {
console.log('Event: ' + message);
});function validateEmail(email, successCallback, errorCallback) {
if (email.includes('@')) {
successCallback('Email is valid');
} else {
errorCallback('Email is invalid');
}
}
validateEmail(
'test@example.com',
(msg) => console.log('Success: ' + msg),
(msg) => console.log('Error: ' + msg)
);function processArray(arr, callback) {
const result = [];
for (let item of arr) {
result.push(callback(item));
}
return result;
}
const numbers = [1, 2, 3];
const doubled = processArray(numbers, x => x * 2);
console.log(doubled); // [2, 4, 6]function doWork(callback) {
const result = 'work done';
// ❌ Error — calling function instead of passing
// callback(result); // if callback is not a function, will be error
// ✅ Correctly — check that callback is a function
if (typeof callback === 'function') {
callback(result);
}
}function asyncOperation(callback) {
// ❌ Error — forgot to call callback
// return 'result';
// ✅ Correctly — call callback
callback('result');
}Callback is a simple but powerful concept that underlies many JavaScript patterns. Modern JavaScript is inconceivable without them! 💪
Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪