The difference between event.preventDefault() and return false in JavaScript depends on how events are handled and whether you’re using vanilla JavaScript or jQuery. Below is a detailed breakdown with examples:
1. Core Differences
Action
event.preventDefault()
return false (Vanilla JS)
return false (jQuery)
Prevents Default
✅ (e.g., stops form submission, link navigation)
✅ (only in inline event handlers)
✅
Stops Propagation
❌ (event bubbles up the DOM)
❌ (in most browsers)
✅ (calls event.stopPropagation())
Works in
Vanilla JS, jQuery, frameworks
Inline HTML event handlers only
jQuery event handlers only
2. Vanilla JavaScript Examples
Example 1: Link Click (Inline Handler vs. addEventListener)
<!-- Inline HTML handler: return false prevents default -->
<a href="https://example.com" onclick="return false;">Inline Link</a>
<!-- addEventListener: return false has no effect -->
<a href="https://example.com" id="vanilla-link">Vanilla JS Link</a>
<script>
document.getElementById("vanilla-link").addEventListener("click", (e) => {
return false; // ❌ Does NOT prevent navigation
e.preventDefault(); // ✅ Use this instead
});
</script>
Example 2: Form Submission
// Vanilla JS: Prevent form submission but allow event bubbling
document.querySelector("form").addEventListener("submit", (e) => {
e.preventDefault(); // ✅ Stops submission
console.log("Form submitted (but not really)");
});
// Inline HTML: return false works
<form onsubmit="return false;">
<button>Submit</button>
</form>
3. jQuery Examples
Example 3: Link Click in jQuery
// jQuery: return false prevents BOTH default and propagation
$("a").click(() => {
console.log("Link clicked, but default and propagation stopped");
return false; // = e.preventDefault() + e.stopPropagation()
});
// Compare to explicit methods:
$("a").click((e) => {
e.preventDefault(); // Allow event to bubble
e.stopPropagation(); // Stop bubbling (optional)
});
// Vanilla JS with addEventListener
document.querySelector("a").addEventListener("click", (e) => {
e.preventDefault(); // Stop navigation
console.log("First listener");
});
document.querySelector("a").addEventListener("click", (e) => {
console.log("Second listener (still runs)"); // Bubbling continues
});
// With return false in jQuery:
$("a").click(() => {
console.log("Only this listener runs");
return false; // Prevents other listeners from firing
});
Scenario 2: Dynamic Actions
// Allow a link to navigate only if a condition is met
document.querySelector("a").addEventListener("click", (e) => {
if (!userIsLoggedIn) {
e.preventDefault(); // Block navigation
alert("Please log in first!");
}
});