Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Event.preventDefault() — canceling the default action

preventDefault() marks an event as canceled to prevent its associated default action. It requires cancelable: true and does not work inside a passive listener.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

event.preventDefault()

event.preventDefault() signals cancellation of an associated action, such as form submission. It returns undefined.

The call sets defaultPrevented to true if cancelable is true and the listener is not passive. Otherwise, it leaves the cancellation state unchanged; checking cancelable alone is insufficient inside a listener with passive: true.

It does not stop propagation or other listeners. Use stopPropagation() and stopImmediatePropagation(), respectively, for those purposes.

The example uses a synthetic event without a built-in default action. It demonstrates cancellation state: dispatchEvent() returns false because the event was canceled. This is a Web API from the DOM Standard, not a feature defined by ECMAScript.

Examples

const target = new EventTarget();
target.addEventListener("demo", (event) => {
  event.preventDefault();
}, { passive: false });

const event = new Event("demo", { cancelable: true });
console.log(event.defaultPrevented);
console.log(target.dispatchEvent(event));
console.log(event.defaultPrevented);

Result

false
false
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources