Introduction to Events

Get a brief introduction to events and learn how they help to add interactivity to the web page.

Up until now, your JavaScript code was executed right from the start. The execution order of statements was determined in advance and only user interactions were passed as input through prompt() calls.

To add more interactivity, the page should react to the user’s actions: clicking on a button, filling a form, etc. In that case, the execution order of statements is not determined in advance anymore, but depends on the user behavior. His actions trigger events that can be handled by writing JavaScript code. This way of writing programs is called event-driven programming. It is often used by user interfaces, and more generally anytime a program needs to interact with a user.

First example

Here’s some starter HTML code and the associated JavaScript code.

Get hands-on with 1400+ tech skills courses.