📖 Scenario: Imagine you are organizing a small event and you want to send invitations. You want to print a message after sending each invitation to confirm it was sent.
🎯 Goal: You will create a simple program that uses a callback function to print a confirmation message after sending each invitation.
📋 What You'll Learn
Create an array of guest names
Create a callback function that prints a confirmation message
Create a function that sends invitations and calls the callback after each send
Call the invitation function and display the confirmation messages
💡 Why This Matters
🌍 Real World
Callbacks are used in real life when you want to do something after a task finishes, like showing a message after a file downloads or a button is clicked.
💼 Career
Understanding callbacks is important for web developers to handle events, asynchronous tasks, and improve user experience.
Progress0 / 4 steps
1
Create the guest list
Create an array called guests with these exact names: 'Alice', 'Bob', 'Charlie'
Javascript
Hint
Use square brackets [] to create an array and separate names with commas.
2
Create the callback function
Create a function called confirmSend that takes one parameter name and prints "Invitation sent to " followed by the name
Javascript
Hint
Use a function declaration and console.log with a template string to print the message.
3
Create the invitation sender function
Create a function called sendInvitations that takes two parameters: guestList and callback. Use a for loop with variable guest to go through guestList and call callback(guest) inside the loop.
Javascript
Hint
Use a for...of loop to go through each guest and call the callback with the guest name.
4
Call the function and show output
Call the function sendInvitations with guests and confirmSend as arguments to print the confirmation messages.
Javascript
Hint
Call sendInvitations(guests, confirmSend); to see the messages.
Practice
(1/5)
1. What is a callback function in JavaScript?
easy
A. A function that runs immediately without waiting
B. A function passed as an argument to another function to be called later
C. A function that only runs once when the program starts
D. A function that cannot be passed to other functions
Solution
Step 1: Understand the role of callbacks
A callback is a function given to another function to run after some work is done.
Step 2: Match the definition
A function passed as an argument to another function to be called later correctly describes this behavior as a function passed as an argument to be called later.
Final Answer:
A function passed as an argument to another function to be called later -> Option B
Quick Check:
Callback = function passed to run later [OK]
Hint: Callbacks are functions given to run after another function finishes [OK]
Common Mistakes:
Thinking callbacks run immediately
Confusing callbacks with event listeners only
Believing callbacks cannot be passed as arguments
2. Which of the following is the correct syntax to use a callback in JavaScript?
easy
A. function greet(callback) { callback }
B. function greet() { callback(); }
C. function greet(callback) { callback; }
D. function greet(callback) { callback(); }
Solution
Step 1: Check how the callback is called
The callback function must be called with parentheses: callback()
Step 2: Identify correct syntax
function greet(callback) { callback(); } correctly defines a function that takes a callback and calls it with callback()
Final Answer:
function greet(callback) { callback(); } -> Option D
Quick Check:
Call callback with parentheses to execute [OK]
Hint: Call callbacks with parentheses to run them [OK]