JavaScript Event

Beginner
8 min read
Updated: Nov 2026
3 examples
DOM events

Definition and Usage

An Event object describes something that happened: a click, a key press, a form submit, and more. Listeners receive that object as their argument.

Useful fields include type and target. Methods such as preventDefault() cancel the browser’s default action, and stopPropagation() stops the event from moving further through the tree.

This page teaches what Event is. Constructor, methods, and properties live under the Event section.

Listeners get the Event object.

button.addEventListener("click", (event) => { ... }) — use event to read target and call preventDefault().

JavaScript Syntax

Common Event patterns:

JavaScript
button.addEventListener("click", function (event) {
  console.log(event.type);   // "click"
  console.log(event.target); // the clicked node
  event.preventDefault();
});

Core Ideas

Ideas every JavaScript Event tutorial builds on:

IdeaExampleRemember
typeevent.typeName like "click"
targetevent.targetOrigin node
currentTargetevent.currentTargetNode with this listener
preventDefaultevent.preventDefault()Cancel default action
stopPropagationevent.stopPropagation()Stop bubbling/capturing
bubblesevent.bubblesWhether it bubbles up

JavaScript Examples

Three short programs that show how Event works in a JS tutorial.

Example 1 — Read the event type

The type string matches the name you listened for.

JavaScript
const event = new Event("save");
console.log(event.type);
console.log(event.bubbles);

How It Works

  1. new Event("save") builds an event named save.
  2. type is "save".
  3. bubbles defaults to false unless you pass options.

Example 2 — Cancel a default action

Call preventDefault inside the listener when you handle the action yourself.

JavaScript
link.addEventListener("click", function (event) {
  event.preventDefault();
  console.log("stayed on page");
});

How It Works

  1. click on a link would navigate by default.
  2. preventDefault() cancels that navigation.
  3. Your code can still run after the call.

Example 3 — Use event.target

target is the node that originated the event.

JavaScript
list.addEventListener("click", function (event) {
  console.log(event.target.tagName);
});

How It Works

  1. The listener is on a parent list.
  2. Clicking a button inside sets target to that button.
  3. tagName shows which kind of node was clicked.

Next: Event Guides

After you know what Event is, open the constructor and method pages.

Look upWhy
ConstructorCreate Event objects
preventDefault()Cancel the default action
stopPropagation()Stop travel through the tree
bubblesWhether the event bubbles

Full lists: Event constructor.

Frequently Asked Questions

An Event is an object that describes something that happened in the page, such as a click. Listeners receive it and can read type, target, and call preventDefault.
target is where the event started. currentTarget is the element whose listener is running now, which may be a parent during bubbling.
When the browser would do something you want to replace, such as following a link or submitting a form, and your code handles it instead.
It stops the event from reaching other listeners on ancestors. Use it sparingly; often preventDefault is enough.
Open the Event constructor, then EventTarget for addEventListener, and MouseEvent for click details.

Open the Event constructor

Then explore preventDefault, stopPropagation, and event properties.

Event constructor →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful