Event Handler

Also known as: event listener, callback

definition

A function that is automatically called when a specific event occurs, such as a button click, key press, or timer tick. All interactive software — websites, apps, games — uses event handlers.

💡 Intuition

An event handler is like setting a trap — you define what should happen, then wait. When the event fires, your code runs automatically.

Core Idea

Event-driven programming reverses the usual flow — instead of your code deciding when to run, external events trigger it.

🔬 Example

button.onClick(function() { alert('Clicked!'); }). The function runs only when the user actually clicks the button, not when the code is first read.

🎯 Why It Matters

All interactive software — websites, apps, games — uses event handlers. They're how programs respond to user actions.

⚠️ Common Confusion

An event handler is registered (set up) once but can fire many times. Setting it up doesn't run it immediately.

Related Concepts

How Event Handler Connects to Other Ideas

To understand event handler, you should first be comfortable with event and function programming.

Go Deeper

Frequently Asked Questions

What is Event Handler in CS Thinking?

A function that is automatically called when a specific event occurs, such as a button click, key press, or timer tick.

Why is Event Handler important?

All interactive software — websites, apps, games — uses event handlers. They're how programs respond to user actions.

What do students usually get wrong about Event Handler?

An event handler is registered (set up) once but can fire many times. Setting it up doesn't run it immediately.

What should I learn before Event Handler?

Before studying Event Handler, you should understand: event, function programming.