What does "javascript:void(0)" mean?
Categories:
Understanding javascript:void(0)
: A Deep Dive into its Purpose and Usage

Explore the meaning and practical applications of javascript:void(0)
in web development, its historical context, and modern alternatives.
In the world of web development, you've likely encountered the peculiar javascript:void(0)
construct. While it might seem cryptic at first glance, it serves a specific purpose, primarily related to preventing default browser actions and executing JavaScript without navigating away from the current page. This article will demystify javascript:void(0)
, explain its historical significance, and discuss more modern and often preferred alternatives.
What void
Operator Does
The void
operator in JavaScript evaluates an expression and then returns undefined
. Its primary use case is to obtain the undefined
primitive value. When used in the context of javascript:void(0)
, it evaluates the expression 0
(which is a number) and then returns undefined
. The key here is that undefined
is not a value that causes the browser to navigate or perform any default action when returned from an href
attribute.
console.log(void 0); // Output: undefined
console.log(void(1 + 2)); // Output: undefined (evaluates 1+2, then returns undefined)
console.log(void 'hello'); // Output: undefined
Demonstration of the void
operator returning undefined
.
The javascript:
Pseudo-Protocol
The javascript:
pseudo-protocol allows you to execute JavaScript code directly within a URL context, such as an <a>
tag's href
attribute. When a browser encounters a URL starting with javascript:
, it executes the code that follows. If the executed code returns a value other than undefined
, the browser might attempt to navigate to that value as if it were a URL, or display it as content. This is where void(0)
becomes crucial.
flowchart TD A[User Clicks Link] --> B{`href` attribute starts with `javascript:`?} B -- Yes --> C[Execute JavaScript code] C --> D{Does code return a value?} D -- Yes --> E{Is returned value `undefined`?} E -- No --> F[Browser attempts to navigate/display returned value] E -- Yes --> G[Browser does nothing (stays on page)] D -- No --> G B -- No --> H[Browser navigates to URL]
Browser behavior when encountering javascript:
in an href
attribute.
Why javascript:void(0)
Was Used (and Why Alternatives are Better)
Historically, javascript:void(0)
was a common pattern to create clickable elements that executed JavaScript without causing a page reload or navigation. This was particularly useful for dynamic interactions before the widespread adoption of modern event handling. For example, an <a>
tag with href="javascript:void(0)"
would prevent the browser from treating the click as a navigation event, allowing an onclick
handler to execute its JavaScript logic without interference.
However, this approach mixes presentation (HTML) with behavior (JavaScript) and can lead to accessibility issues. Screen readers might announce these links as actual navigation targets, and users who rely on keyboard navigation might find unexpected behavior. Modern web development practices advocate for a clear separation of concerns.
<!-- Old approach -->
<a href="javascript:void(0);" onclick="myFunction();">Click Me (Old Way)</a>
<!-- Modern approach -->
<button onclick="myFunction();">Click Me (Modern Way)</button>
<a href="#" id="myLink">Click Me (Modern Link)</a>
<script>
function myFunction() {
alert('Function executed!');
}
// For the modern link approach, prevent default behavior
document.getElementById('myLink').addEventListener('click', function(event) {
event.preventDefault(); // Stop the browser from navigating to '#' or reloading
myFunction();
});
</script>
Comparison of javascript:void(0)
with modern event handling.
<button>
tags instead of <a>
tags with javascript:void(0)
. Buttons are semantically correct for actions and inherently prevent default navigation.javascript:void(0)
in new code. It's generally considered an anti-pattern due to accessibility concerns and the availability of better, more semantic alternatives.