JavaScript Callbacks: Function Parameters
Understand JavaScript callbacks. Learn how functions can be passed as arguments and executed later.
JavaScript Callbacks
What are Callbacks?
Callbacks are functions passed as arguments to other functions. They execute later.
``` ┌─────────────────────┐ │ Function A │ │ ┌───────────────┐ │ │ │ callback() │ │ ← Function passed in │ └───────────────┘ │ └─────────────────────┘ ```
Simple Example
```javascript function greet(name, callback) { console.log(`Hello, ${name}`); callback(); }
greet('John', () => { console.log('Callback executed!'); }); ```
Common Use Cases
```javascript setTimeout(() => { console.log('Delayed'); }, 1000);
array.forEach(item => { console.log(item); }); ```
Key Takeaway
Callbacks are functions passed as arguments. Execute after main function completes. Used in timers, array methods, and async operations.