A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of action.
Click to reveal answer
beginner
Why do we use callbacks in JavaScript?
Callbacks let us run code after another function finishes, especially for tasks that take time like loading data, so the program doesn’t stop and wait.
Click to reveal answer
beginner
How do you define and use a callback function? Show a simple example.
You define a function and pass it as an argument to another function. For example:<br><pre>function greet(name) {
console.log('Hello ' + name);
}
function processUserInput(callback) {
const name = 'Alice';
callback(name);
}
processUserInput(greet);</pre>
Click to reveal answer
beginner
What problem can callbacks help solve in JavaScript?
Callbacks help manage tasks that take time, like reading files or fetching data, so the program can keep running without waiting and freezing.
Click to reveal answer
intermediate
What is "callback hell" and why should it be avoided?
"Callback hell" happens when callbacks are nested inside callbacks many times, making code hard to read and maintain. It’s better to use promises or async/await to keep code clean.
Click to reveal answer
What is a callback function?
AA function that cannot be called
BA function that returns a value
CA function passed as an argument to another function
DA function that runs only once
✗ Incorrect
A callback is a function passed into another function to be called later.
Why are callbacks useful in JavaScript?
ATo stop the program until a task finishes
BTo run code after a task finishes without stopping the program
CTo make the program run slower
DTo create new variables
✗ Incorrect
Callbacks let JavaScript run other code while waiting for tasks like data loading.
Which of these is an example of passing a callback?
AsetTimeout(() => console.log('Hi'), 1000)
Blet x = 5 + 3
Cfunction add(a, b) { return a + b; }
Dconsole.log('Hello')
✗ Incorrect
setTimeout takes a function as a callback to run after a delay.
What is a common downside of using many nested callbacks?
ACode runs faster
BCode becomes shorter
CCode uses less memory
DCode becomes hard to read and maintain
✗ Incorrect
Many nested callbacks create "callback hell," making code confusing.
Which modern JavaScript feature helps avoid callback hell?
APromises and async/await
BGlobal variables
CLoops
DAlerts
✗ Incorrect
Promises and async/await make asynchronous code easier to read and write.
Explain what a callback function is and why it is useful in JavaScript.
Think about how JavaScript runs code that takes time without stopping everything.
You got /3 concepts.
Describe what "callback hell" is and how you can avoid it.
Imagine stacking many callbacks inside each other and how that looks.
You got /3 concepts.
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]