Example 1 — Read event.type
Click a button and log the type name.
button.addEventListener("click", (event) => {
console.log(event.type); // "click"
}); How It Works
The string matches the name you registered with addEventListener.

The type property is a read-only string naming the event—like click, load, or error. It is set when the event is constructed. Learn MDN’s keyboard/mouse logger, shared handlers, and custom event names—with five examples and try-it labs.
Instance property
string
Event name
Constructed
addEventListener
Baseline widely
When you write element.addEventListener("click", handler), the string "click" is the event type. Inside the handler, event.type is that same name for the event that actually fired.
That is useful when one function handles several kinds of input—for example logging every keyboard and mouse stage, or branching on click vs keydown in shared UI code.
Event type strings are case-sensitive. Use "click", not "Click".
Event.typeAn instance property that returns a string containing the event’s type—the name commonly used to refer to that event.
click, load, error, keydown, submit.new Event("name") events.event.type = … later.addEventListener.event.type A string containing the type of the Event.
button.addEventListener("click", (event) => {
console.log(event.type); // "click"
}); | Goal | Code / note |
|---|---|
| Read name | event.type |
| Branch | if (event.type === "click") |
| Listen | addEventListener("click", fn) |
| Custom | new Event("save") → type "save" |
| MDN status | Baseline Widely available (since July 2015) |
Four facts to remember about Event.type.
stringRead-only
clickload, error…
exactCase-sensitive
widelySince Jul 2015
Examples follow MDN Event: type. Use View Output or Try It Yourself.
Read the type string and log several input events.
event.typeClick a button and log the type name.
button.addEventListener("click", (event) => {
console.log(event.type); // "click"
}); The string matches the name you registered with addEventListener.
One function prepends event.type for keys and mouse buttons.
function getEventType(event) {
const log = document.getElementById("log");
log.innerText = `${event.type}\n${log.innerText}`;
}
document.addEventListener("keydown", getEventType);
document.addEventListener("keypress", getEventType);
document.addEventListener("keyup", getEventType);
document.addEventListener("mousedown", getEventType);
document.addEventListener("mouseup", getEventType);
document.addEventListener("click", getEventType); A single click often fires mousedown, then mouseup, then click—each with its own type.
Branch on type, invent custom events, and watch form events.
switchOne function for both click and keydown.
function onInput(event) {
switch (event.type) {
case "click":
out.textContent = "Mouse click";
break;
case "keydown":
out.textContent = "Key: " + event.key;
break;
default:
out.textContent = "Other: " + event.type;
}
}
button.addEventListener("click", onInput);
window.addEventListener("keydown", onInput); event.type lets one function stay tidy instead of copying logic into two listeners.
Construct and dispatch an event named save.
const target = new EventTarget();
target.addEventListener("save", (event) => {
console.log(event.type); // "save"
});
target.dispatchEvent(new Event("save")); The first argument to new Event(…) becomes event.type.
input vs changeLog which form event fired as you type and leave the field.
function logType(event) {
out.textContent = event.type + " → " + event.target.value;
}
field.addEventListener("input", logType);
field.addEventListener("change", logType); input fires continuously; change usually fires when the value is committed (for example on blur).
save, cart:update).addEventListener and the event object.Built-in UI action or new Event("name").
The name string is stored on the event object.
addEventListener("click", …) only gets click events.
Shared functions branch on event.type.
Event.type is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. It is also available in Web Workers.
Read-only string: the event’s name (click, load, error, custom names, and more).
Bottom line: Use event.type to know which kind of event fired—especially in shared handlers and custom events.
Event.type is the event’s name as a string. It connects addEventListener, constructed events, and shared handlers that need to know what fired.
Continue with composedPath(), timeStamp, target, or the JavaScript hub.
save, cart:add)type while learning event sequencesevent.type after creationClick vs click)typeThe event’s name string—set once, read often.
read-only
APIclick, load…
Meaningswitch on type
Patternnew Event(name)
Createsince Jul 2015
StatusA physical mouse click is not one event type—it is usually a short sequence (mousedown, mouseup, click). Logging event.type is one of the easiest ways to see that sequence for yourself.
See the full EventTarget array the event travels through.
5 people found this page helpful