Event Types Get Certified
JavaScript provides a rich set of event types that cover almost every kind of user interaction and browser activity. Choosing the right event type is essential for building responsive and accessible interfaces.
This lesson is a practical reference of the most important event types, grouped by category, with short simulated examples so you can see the data each event carries.
Mouse Events
click / dblclick
What this code does: Click and double-click are the most common mouse events.
const handleClick = function(event) {
console.log('Click at', event.clientX, event.clientY);
};
const handleDblClick = function(event) {
console.log('Double-click detected');
};
handleClick({ clientX: 120, clientY: 80 });
handleDblClick({});
const handleClick = function(event) { stores a function in handleClick. A function like this is an event handler: it is meant to run when an event happens. It receives one parameter, event, an object that describes what happened. In a real page the browser creates the event object for you and passes it to the handler automatically; here the handler is called by hand with a small fake object so you can see how it behaves.console.log('Click at', event.clientX, event.clientY); prints the text and the two coordinates. For mouse events, clientX and clientY give the pointer’s position in pixels, measured from the top-left corner of the visible page area.}; closes the function and ends the assignment.const handleDblClick = function(event) { defines a second handler, for the dblclick event, which fires when the user clicks twice quickly.console.log('Double-click detected'); only reports that it happened. It does not use any event data.}; closes the function. Line 7 is a blank separator line.handleClick({ clientX: 120, clientY: 80 }); calls the handler with a fake event object holding the coordinates 120 and 80. The output is Click at 120 80.handleDblClick({}); calls the handler with an empty object, because it needs no data. The output is Double-click detected.mousedown / mouseup
What this code does: These fire when a mouse button is pressed or released.
const handleMouseDown = e => console.log('Button pressed:', e.button);
const handleMouseUp = e => console.log('Button released');
handleMouseDown({ button: 0 }); // left button
handleMouseUp({});
const handleMouseDown = e => console.log('Button pressed:', e.button); is a short arrow-function handler for mousedown, which fires the moment a mouse button is pressed down. The parameter is named e, a common short name for the event. e.button tells which button: 0 is the left button, 1 the middle button and 2 the right button.const handleMouseUp = e => console.log('Button released'); is the handler for mouseup, which fires when the button is released again. (The extra spaces before = only line the code up with the previous line.) Line 3 is a blank separator line.handleMouseDown({ button: 0 }); calls the handler with a fake event whose button is 0. The comment // left button explains the number. The output is Button pressed: 0.handleMouseUp({}); calls the second handler. The output is Button released. A full click is the sequence mousedown, mouseup, then click.mouseenter / mouseleave
What this code does: Prefer these over mouseover/mouseout when you do not want bubbling.
const handleEnter = () => console.log('Mouse entered element');
const handleLeave = () => console.log('Mouse left element');
handleEnter();
handleLeave();
const handleEnter = () => console.log('Mouse entered element'); is a handler for mouseenter, which fires when the pointer moves onto an element. It takes no parameters, because it does not need any event data.const handleLeave = () => console.log('Mouse left element'); is the handler for mouseleave, which fires when the pointer moves off the element. Unlike mouseover and mouseout, these two events do not bubble, and do not fire again when the pointer moves between the element’s children. Line 3 is a blank separator line.handleEnter(); calls the handler. The output is Mouse entered element.handleLeave(); calls the handler. The output is Mouse left element.mousemove
What this code does: Fires continuously while the pointer moves over an element.
const handleMove = function(event) {
console.log(`Pointer at (${event.clientX}, ${event.clientY})`);
};
handleMove({ clientX: 240, clientY: 160 });
const handleMove = function(event) { defines a handler for mousemove, which fires over and over while the pointer moves over an element. Because it fires so often, handlers for this event should be short and fast.console.log(`Pointer at (${event.clientX}, ${event.clientY})`); uses a template literal: text in backticks, with ${ ... } placeholders that are replaced by the values inside. It is a neater alternative to joining pieces with +.}; closes the function and ends the assignment.handleMove({ clientX: 240, clientY: 160 }); calls the handler with fake coordinates. The output is Pointer at (240, 160).Keyboard Events
keydown / keyup
What this code does: keydown fires when a key is pressed; keyup when it is released.
const handleKeyDown = function(event) {
console.log('Key down:', event.key);
if (event.ctrlKey) console.log('Ctrl is held');
};
const handleKeyUp = function(event) {
console.log('Key up:', event.key);
};
handleKeyDown({ key: 'Enter', ctrlKey: true });
handleKeyUp({ key: 'Enter' });
const handleKeyDown = function(event) { defines a handler for keydown, which fires when a key is pressed (and repeats while the key is held).console.log('Key down:', event.key); prints event.key, the value of the pressed key, such as 'a', 'Enter' or 'ArrowUp'.if (event.ctrlKey) console.log('Ctrl is held'); uses event.ctrlKey, which is true if the Ctrl key was held down at the same time. The if has no braces because it controls one statement. There are matching properties shiftKey, altKey and metaKey.}; closes the function and ends the assignment.const handleKeyUp = function(event) { defines a handler for keyup, which fires once when the key is released.console.log('Key up:', event.key); prints which key was released.}; closes the function. Line 8 is a blank separator line.handleKeyDown({ key: 'Enter', ctrlKey: true }); passes a fake event for the Enter key pressed with Ctrl. The output is Key down: Enter and Ctrl is held.handleKeyUp({ key: 'Enter' }); prints Key up: Enter.Form Events
input / change
What this code does: input fires on every change; change fires when the value is committed.
const handleInput = e => console.log('Live value:', e.target.value);
const handleChange = e => console.log('Committed value:', e.target.value);
handleInput({ target: { value: 'Hel' } });
handleInput({ target: { value: 'Hello' } });
handleChange({ target: { value: 'Hello' } });
const handleInput = e => console.log('Live value:', e.target.value); is a handler for input, which fires on every change to a field’s value, for example on each keystroke. e.target is the element that fired the event (the form field), and .value is its current text.const handleChange = e => console.log('Committed value:', e.target.value); is a handler for change, which fires only when the value is committed: for a text field, when it loses focus or the user presses Enter. Line 3 is a blank separator line.handleInput({ target: { value: 'Hel' } }); simulates the user having typed Hel. The output is Live value: Hel.handleInput({ target: { value: 'Hello' } }); simulates the next keystrokes. The output is Live value: Hello. The input event fired each time.handleChange({ target: { value: 'Hello' } }); simulates leaving the field. The output is Committed value: Hello. It fires only once, at the end.submit / reset
What this code does: submit fires when a form is submitted; reset when it is cleared.
const handleSubmit = function(event) {
if (event.preventDefault) event.preventDefault();
console.log('Form submitted (default prevented)');
};
const handleReset = () => console.log('Form was reset');
handleSubmit({ preventDefault() {} });
handleReset();
const handleSubmit = function(event) { defines a handler for submit, which fires when a form is submitted.if (event.preventDefault) event.preventDefault(); cancels the browser’s default behaviour, which for a form is to send the data and reload the page. The if first checks that the method exists, which makes it safe to use with the fake object in this example.console.log('Form submitted (default prevented)'); prints a confirmation. This is where real code would validate the data and send it with JavaScript.}; closes the function and ends the assignment.const handleReset = () => console.log('Form was reset'); is a handler for reset, which fires when a form’s reset button is used to clear the fields. Line 6 is a blank separator line.handleSubmit({ preventDefault() {} }); passes a fake event with an empty preventDefault() method (written in the short method form). The output is Form submitted (default prevented).handleReset(); prints Form was reset.focus / blur
What this code does: focus and blur do not bubble. Use focusin/focusout when you need bubbling.
const handleFocus = e => console.log('Focused:', e.target.id);
const handleBlur = e => console.log('Blurred:', e.target.id);
handleFocus({ target: { id: 'email' } });
handleBlur({ target: { id: 'email' } });
const handleFocus = e => console.log('Focused:', e.target.id); is a handler for focus, which fires when an element, such as an input field, receives focus (the user clicks into it or tabs to it). e.target.id is the id of that element.const handleBlur = e => console.log('Blurred:', e.target.id); is a handler for blur, which fires when the element loses focus. Neither focus nor blur bubbles. If you need bubbling (for example to listen on a whole form), use focusin and focusout instead. Line 3 is a blank separator line.handleFocus({ target: { id: 'email' } }); simulates the user clicking into a field with the id email. The output is Focused: email.handleBlur({ target: { id: 'email' } }); simulates leaving that field. The output is Blurred: email.Drag Events
What this code does: Basic drag-and-drop lifecycle: start → drag → drop → end.
const handleDragStart = e => console.log('Drag started on', e.target.id);
const handleDrag = () => console.log('Dragging…');
const handleDrop = e => console.log('Dropped on', e.target.id);
const handleDragEnd = () => console.log('Drag ended');
handleDragStart({ target: { id: 'item-1' } });
handleDrag();
handleDrop({ target: { id: 'drop-zone' } });
handleDragEnd();
const handleDragStart = e => console.log('Drag started on', e.target.id); is the handler for dragstart, which fires once on the element the user begins dragging.const handleDrag = () => console.log('Dragging…'); is the handler for drag, which fires repeatedly while the element is being dragged.const handleDrop = e => console.log('Dropped on', e.target.id); is the handler for drop, which fires on the element where the dragged item is released. (Real code also needs to cancel the default action of dragover to allow dropping.)const handleDragEnd = () => console.log('Drag ended'); is the handler for dragend, which fires on the dragged element when the drag operation finishes, whether it was dropped or not. Line 5 is a blank separator line.handleDragStart({ target: { id: 'item-1' } }); prints Drag started on item-1.handleDrag(); prints Dragging….handleDrop({ target: { id: 'drop-zone' } }); prints Dropped on drop-zone.handleDragEnd(); prints Drag ended. The four lines of output show the lifecycle: start, drag, drop, end.Window Events
load / resize / scroll
What this code does: Common window-level events for page readiness and layout changes.
const handleLoad = () => console.log('Page fully loaded');
const handleResize = e => console.log(`Window: ${e.innerWidth}×${e.innerHeight}`);
const handleScroll = e => console.log('Scroll Y:', e.scrollY);
handleLoad();
handleResize({ innerWidth: 1280, innerHeight: 720 });
handleScroll({ scrollY: 340 });
const handleLoad = () => console.log('Page fully loaded'); is a handler for load, which fires on the window when the whole page, including images and styles, has finished loading.const handleResize = e => console.log(`Window: ${e.innerWidth}×${e.innerHeight}`); is a handler for resize, which fires when the browser window changes size. It builds its message with a template literal. Note: in a real page you would read window.innerWidth and window.innerHeight; here the numbers are passed in the fake event object to keep the example simple.const handleScroll = e => console.log('Scroll Y:', e.scrollY); is a handler for scroll, which fires as the page is scrolled. In a real page the scroll position is window.scrollY. Line 4 is a blank separator line.handleLoad(); prints Page fully loaded.handleResize({ innerWidth: 1280, innerHeight: 720 }); prints Window: 1280×720.handleScroll({ scrollY: 340 }); prints Scroll Y: 340.Touch Events
What this code does: Basic touchstart / touchend for mobile support.
const handleTouchStart = e => {
console.log('Touch began');
console.log('Touches:', e.touches ? e.touches.length : 1);
};
const handleTouchEnd = () => console.log('Touch ended');
handleTouchStart({ touches: [1, 2] });
handleTouchEnd();
const handleTouchStart = e => { defines a handler for touchstart, which fires when a finger touches the screen. This arrow function has a body in curly braces because it contains two statements.console.log('Touch began'); prints a message when the touch starts.console.log('Touches:', e.touches ? e.touches.length : 1); uses a ternary operator. e.touches is a list of all the fingers currently on the screen. If it exists, the count e.touches.length is printed; otherwise 1.}; closes the arrow function body and ends the assignment.const handleTouchEnd = () => console.log('Touch ended'); is a handler for touchend, which fires when a finger is lifted. Line 6 is a blank separator line.handleTouchStart({ touches: [1, 2] }); passes a fake event with two touch points. The output is Touch began and Touches: 2.handleTouchEnd(); prints Touch ended.Media Events
play
What this code does: Play fires when media (audio/video) starts or resumes playback.
// Play event
const handlePlay = function(event) {
console.log('Media playback started');
console.log('Current time:', event.target.currentTime);
};
const playEvent = {
type: 'play',
target: {
currentTime: 0,
duration: 120
}
};
handlePlay(playEvent);
// Play event is a comment naming the event. JavaScript ignores it.const handlePlay = function(event) { defines a handler for play, which fires when an audio or video element starts or resumes playing. For media events, event.target is the media element.console.log('Media playback started'); prints a message. The output is Media playback started.console.log('Current time:', event.target.currentTime); prints the playback position in seconds. For a real element, currentTime is a property of the media element.}; closes the function. Line 6 is a blank separator line.const playEvent = { starts an object that imitates the event the browser would create.type: 'play', names the event type, as a real event does.target: { starts a nested object that stands for the media element.currentTime: 0, says that playback is at the very start, 0 seconds.duration: 120 says that the media is 120 seconds long.} closes the nested target object.}; closes the fake event and ends the statement. Line 14 is a blank separator line.handlePlay(playEvent); passes the fake event to the handler. The output is Media playback started and Current time: 0.pause
What this code does: Pause fires when media playback is paused.
// Pause event
const handlePause = function(event) {
console.log('Media playback paused');
console.log('Paused at:', event.target.currentTime, 'seconds');
};
const pauseEvent = {
type: 'pause',
target: {
currentTime: 45.5,
duration: 120
}
};
handlePause(pauseEvent);
// Pause event is a comment naming the event.const handlePause = function(event) { defines a handler for pause, which fires when playback is paused. It also fires when a video reaches its end, just before the ended event.console.log('Media playback paused'); prints Media playback paused.console.log('Paused at:', event.target.currentTime, 'seconds'); prints the playback position from the media element, followed by the word seconds.}; closes the function. Line 6 is a blank separator line.const pauseEvent = { starts an object that imitates the event.type: 'pause', names the event type.target: { starts the object that stands for the media element.currentTime: 45.5, says that playback was paused at 45.5 seconds.duration: 120 says the media is 120 seconds long.} closes the nested target object.}; closes the fake event. Line 14 is a blank separator line.handlePause(pauseEvent); prints Media playback paused and Paused at: 45.5 seconds.ended
What this code does: Ended fires when media reaches the end of its duration.
// Ended event
const handleEnded = function(event) {
console.log('Media playback ended');
console.log('Duration:', event.target.duration, 'seconds');
};
const endedEvent = {
type: 'ended',
target: {
currentTime: 120,
duration: 120
}
};
handleEnded(endedEvent);
// Ended event is a comment naming the event.const handleEnded = function(event) { defines a handler for ended, which fires when playback reaches the end of the media.console.log('Media playback ended'); prints Media playback ended.console.log('Duration:', event.target.duration, 'seconds'); prints the media’s total length in seconds.}; closes the function. Line 6 is a blank separator line.const endedEvent = { starts an object that imitates the event.type: 'ended', names the event type.target: { starts the object for the media element.currentTime: 120, shows that the position is at the very end: it equals the duration.duration: 120 is the total length in seconds.} closes the nested target object.}; closes the fake event. Line 14 is a blank separator line.handleEnded(endedEvent); prints Media playback ended and Duration: 120 seconds.volumechange
What this code does: Volumechange fires when the media volume or mute state changes.
// Volumechange event
const handleVolumeChange = function(event) {
console.log('Volume changed');
console.log('Volume:', event.target.volume);
console.log('Muted:', event.target.muted);
};
const volumeChangeEvent = {
type: 'volumechange',
target: {
volume: 0.7,
muted: false
}
};
handleVolumeChange(volumeChangeEvent);
// Volumechange event is a comment naming the event.const handleVolumeChange = function(event) { defines a handler for volumechange, which fires when the volume is changed or the media is muted or unmuted.console.log('Volume changed'); prints Volume changed.console.log('Volume:', event.target.volume); prints the current volume, a number from 0 (silent) to 1 (full).console.log('Muted:', event.target.muted); prints true or false, depending on whether the media is muted.}; closes the function. Line 7 is a blank separator line.const volumeChangeEvent = { starts an object that imitates the event.type: 'volumechange', names the event type.target: { starts the object for the media element.volume: 0.7, sets the volume to 70%.muted: false says that the sound is not muted.} closes the nested target object.}; closes the fake event. Line 15 is a blank separator line.handleVolumeChange(volumeChangeEvent); prints Volume changed, Volume: 0.7 and Muted: false.Event Types Reference Table
| Category | Event Type | Description | Bubbles |
|---|---|---|---|
| Mouse | click | User clicks on element | Yes |
| Mouse | dblclick | User double-clicks | Yes |
| Mouse | mousedown | Mouse button pressed | Yes |
| Mouse | mouseup | Mouse button released | Yes |
| Mouse | mouseenter | Mouse enters element | No |
| Mouse | mouseleave | Mouse leaves element | No |
| Mouse | mouseover | Mouse over element | Yes |
| Mouse | mouseout | Mouse out of element | Yes |
| Mouse | mousemove | Mouse moves | Yes |
| Keyboard | keydown | Key pressed down | Yes |
| Keyboard | keyup | Key released | Yes |
| Form | submit | Form submitted | Yes |
| Form | reset | Form reset | Yes |
| Form | input | Input value changes | Yes |
| Form | change | Value changes and loses focus | Yes |
| Form | focus | Element receives focus | No |
| Form | blur | Element loses focus | No |
| Drag | drag | Element is being dragged | Yes |
| Drag | dragstart | Drag operation starts | Yes |
| Drag | dragend | Drag operation ends | Yes |
| Drag | drop | Element dropped | Yes |
| Window | load | Page fully loaded | No |
| Window | resize | Window resized | No |
| Window | scroll | Page scrolled | No |
| Touch | touchstart | Touch begins | Yes |
| Touch | touchend | Touch ends | Yes |
| Media | play | Media starts playing | No |
| Media | pause | Media paused | No |
Practical Example: Event Type Detection
What this code does: A reusable logger that prints type, target, and other event details.
// Universal event logger
const logEvent = function(event) {
console.log('='.repeat(40));
console.log('Event Type:', event.type);
console.log('Target:', event.target ? event.target.id : 'N/A');
console.log('Timestamp:', event.timeStamp || Date.now());
// Mouse events
if (event.clientX !== undefined) {
console.log('Mouse Position:', event.clientX, event.clientY);
}
// Keyboard events
if (event.key) {
console.log('Key:', event.key);
console.log('Modifiers - Ctrl:', event.ctrlKey, 'Shift:', event.shiftKey);
}
// Form events
if (event.target && event.target.value !== undefined) {
console.log('Input Value:', event.target.value);
}
console.log('='.repeat(40));
};
// Simulate different event types
const events = [
{ type: 'click', clientX: 100, clientY: 150, target: { id: 'btn1' }, timeStamp: 1000 },
{ type: 'keydown', key: 'Enter', ctrlKey: true, shiftKey: false, target: { id: 'input1' }, timeStamp: 2000 },
{ type: 'input', target: { id: 'nameField', value: 'John Doe' }, timeStamp: 3000 }
];
console.log('Testing multiple event types:');
console.log('');
events.forEach(logEvent);
// Universal event logger is a comment describing the purpose: one function that can log any kind of event.const logEvent = function(event) { defines a function that receives an event object.console.log('='.repeat(40)); builds a line of 40 equals signs with the string method repeat(), and prints it to separate one event’s report from the next.console.log('Event Type:', event.type); prints the event’s type, such as click or keydown. Every event has this property.console.log('Target:', event.target ? event.target.id : 'N/A'); uses a ternary operator: if the event has a target, print its id; otherwise print N/A.console.log('Timestamp:', event.timeStamp || Date.now()); prints the event’s own timeStamp, and if that is missing, falls back to the current time from Date.now() using the || (OR) operator. Line 7 is a blank separator line.// Mouse events labels the next block.if (event.clientX !== undefined) { checks whether the event has a clientX value. Mouse events have one, so this is a simple way to recognise them.console.log('Mouse Position:', event.clientX, event.clientY); prints the coordinates. This runs only for the click event.} closes the if block. Line 12 is a blank separator line.// Keyboard events labels the next block.if (event.key) { is true when the event has a key value, which is the case for keyboard events.console.log('Key:', event.key); prints the key that was pressed.console.log('Modifiers - Ctrl:', event.ctrlKey, 'Shift:', event.shiftKey); prints whether Ctrl and Shift were held down.} closes the if block. Line 18 is a blank separator line.// Form events labels the next block.if (event.target && event.target.value !== undefined) { uses && (AND): first it checks that event.target exists (to avoid an error when it is missing), and then that the target has a value, as form fields do.console.log('Input Value:', event.target.value); prints the field’s text.} closes the if block. Line 23 is a blank separator line.console.log('='.repeat(40)); prints another line of 40 equals signs to end the report.}; closes the function and ends the assignment. Line 26 is a blank separator line.// Simulate different event types explains that the next lines create fake events.const events = [ starts an array of three fake event objects.{ type: 'click', clientX: 100, clientY: 150, target: { id: 'btn1' }, timeStamp: 1000 }, imitates a click at (100, 150) on an element with the id btn1.{ type: 'keydown', key: 'Enter', ctrlKey: true, shiftKey: false, ... }, imitates pressing Enter with Ctrl held.{ type: 'input', target: { id: 'nameField', value: 'John Doe' }, ... } imitates typing in a field. It is the last item, so no comma follows.]; closes the array. Line 33 is a blank separator line.console.log('Testing multiple event types:'); prints Testing multiple event types:.console.log(''); prints an empty line for spacing. Line 36 is a blank separator line.events.forEach(logEvent); passes the function logEvent to forEach(), which calls it once for each event. Each report has a line of equals signs, the type, target and timestamp, plus the extra details for its kind: the click shows Mouse Position: 100 150, the keydown shows Key: Enter and Modifiers - Ctrl: true Shift: false, and the input shows Input Value: John Doe.Best Practices for Event Types
- Choose the most specific event type for your use case.
- Use
mouseenter/mouseleaveinstead ofmouseover/mouseoutwhen you don’t want bubbling. - Prefer
inputoverkeyupfor text input fields to capture all changes. - Use
focusin/focusoutwhen you need bubbling behavior. - Be aware of which events bubble and which don’t.
- Use passive event listeners for scroll and touch events to improve performance.
- Consider touch events for mobile-friendly applications.
- Clean up event listeners when they’re no longer needed.
Common Mistakes With Event Types
- Using
mouseoverwhenmouseenterwould be better (avoiding unwanted bubbling). - Forgetting that some events don’t bubble (like
focusandblur). - Using deprecated events like
keypress. - Not preventing default behavior when necessary (e.g., form submit, link click).
- Attaching expensive handlers to frequently-firing events (like
scrollormousemove). - Not considering mobile touch events in addition to mouse events.
Quick Summary
- JavaScript provides many event types for different interactions.
- Mouse events include
click,dblclick,mouseenter,mouseleave, and more. - Keyboard events are
keydownandkeyup. - Form events include
submit,input,change,focus, andblur. - Drag events enable drag-and-drop functionality.
- Window events respond to browser window actions.
- Touch events handle mobile device interactions.
- Animation and transition events track CSS animations.
- Media events control audio and video playback.
- Some events bubble, others don’t.
- Choose the right event type for your specific use case.
Try It Yourself
Experiment with different event types. Create handlers for various events and see how they work.
What this code does: Practice: runs a set of simulated events through a shared handler to compare types.
// Event type testing
const events = {
click: { type: 'click', clientX: 100, clientY: 150 },
keydown: { type: 'keydown', key: 'a', ctrlKey: false },
input: { type: 'input', target: { value: 'test' } },
submit: { type: 'submit', target: { id: 'form1' } },
mouseenter: { type: 'mouseenter', target: { id: 'box1' } }
};
const handleEvent = function(event) {
console.log('Event:', event.type);
console.log('Data:', JSON.stringify(event, null, 2));
console.log('---');
};
console.log('Testing different event types:');
console.log('');
Object.keys(events).forEach(function(key) {
handleEvent(events[key]);
});
// Event type testing is a comment describing the example.const events = { starts an object whose properties are fake events, one for each type. Try adding your own event type here.click: { type: 'click', clientX: 100, clientY: 150 }, is a fake mouse event with coordinates.keydown: { type: 'keydown', key: 'a', ctrlKey: false }, is a fake keyboard event for the letter a.input: { type: 'input', target: { value: 'test' } }, is a fake input event whose target holds the text test.submit: { type: 'submit', target: { id: 'form1' } }, is a fake form submission.mouseenter: { type: 'mouseenter', target: { id: 'box1' } } is a fake mouse-enter event. It is the last property, so there is no comma.}; closes the object. Line 9 is a blank separator line.const handleEvent = function(event) { defines one handler that will be used for all the events.console.log('Event:', event.type); prints Event: and the type, such as click.console.log('Data:', JSON.stringify(event, null, 2)); converts the whole event object into readable JSON text. The 2 means indent with two spaces, so that the properties are shown one per line.console.log('---'); prints a divider between events.}; closes the function. Line 15 is a blank separator line.console.log('Testing different event types:'); prints a heading.console.log(''); prints an empty line. Line 18 is a blank separator line.Object.keys(events).forEach(function(key) { gets the list of property names of the events object (click, keydown, input, submit, mouseenter), and calls the function once for each name, passing it as key.handleEvent(events[key]); looks up the event by its name and passes it to the shared handler. The output shows, for each of the five events, the type line, the data as JSON, and a divider.}); closes the function, the call to forEach() and the statement.Tip: Understanding all the different event types available in JavaScript will help you build more interactive and responsive web applications. Each event type serves a specific purpose, so choose wisely based on your needs.
Mastering JavaScript event types is essential for creating dynamic user interfaces. Knowing when and how to use each event type will make you a more effective developer.
🧪 Test Your JavaScript Code
Edit the code on the left and click “Run Code” to see the console output on the right.
Click “Run Code” to see the output here.
Code Explanation
The complete example stays visible. Use Previous and Next to move through its logical blocks.
Select Next to begin exploring the code.