What is a Callback Function?
A callback function is a function that is passed as an argument to another function and is executed later.
Simple Definition
“A function inside another function”
Callback Function – Real-Life Examples
1. Washing Clothes
function washClothes(callback) {
console.log("Clothes are washed");
callback();
}
function dryClothes() {
console.log("Clothes are drying");
}
washClothes(dryClothes);
Explanation:
First → clothes are washed
Then → drying happens
Output:
Clothes are washed
Clothes are drying
2. Cooking Food
function cookFood(callback) {
console.log("Cooking food...");
callback();
}
function serveFood() {
console.log("Food is served");
}
cookFood(serveFood);
First cooking → then serving
3. Mobile Charging
function chargePhone(callback) {
console.log("Phone is charging...");
callback();
}
function usePhone() {
console.log("Now you can use the phone");
}
chargePhone(usePhone);
First charge → then use
4. Studying
function study(callback) {
console.log("Studying...");
callback();
}
function takeTest() {
console.log("Taking test");
}
study(takeTest);
First study → then test
5. Login System
function login(callback) {
console.log("User logged in");
callback();
}
function showDashboard() {
console.log("Welcome to dashboard");
}
login(showDashboard);
First login → then dashboard
Top comments (0)