Anyone who has spent time browsing a website’s source code, or debugging a broken link, has likely run into the curious little snippet javascript:void(0).
It shows up constantly in href attributes across the web, often confusing developers encountering it for the first time, and occasionally confusing regular users who see a strange “javascript:void(0)” message appear on a page that should have navigated somewhere. This article explains exactly what it means, why it exists, how it’s used, and whether it still belongs in modern web development.
Breaking Down the Syntax
To understand javascript:void(0), it helps to split it into its two separate parts: the javascript: prefix and the void(0) expression.
The javascript: prefix is what’s known as a pseudo URL. Rather than pointing to an actual web address, it tells the browser to treat whatever follows as JavaScript code to execute directly, rather than a destination to navigate to. This allows a link to run a piece of JavaScript instead of loading a new page.
The void(0) portion is where the actual behavior comes from. void is a unary operator in JavaScript, meaning it operates on a single value. When applied to any expression, including the number 0, it evaluates that expression and then deliberately discards the result, always returning the value undefined instead. The number 0 inside the parentheses has no special meaning of its own; it’s simply a common, conventional placeholder. You could just as easily write void(1), void(“anything”), or void(true), and the result would still be undefined.
Put together, javascript:void(0) tells the browser to run a tiny piece of JavaScript that does nothing and returns undefined, with no visible effect on the page whatsoever.
Why Developers Use It
The most common use case for javascript:void(0) is inside the href attribute of an anchor tag. Normally, clicking a link causes the browser to navigate to whatever URL is specified in that attribute.
But sometimes a developer wants an element that looks and behaves like a clickable link, triggering some JavaScript function, showing a dropdown, opening a modal, without actually navigating anywhere or reloading the page.
By setting href=”javascript:void(0)”, the link effectively becomes inactive from a navigation standpoint, while still allowing a separate onclick handler to perform whatever actual action is intended. A typical example looks like this:
HTML:
Here, clicking the link runs the performAction() function without the browser attempting to load a new page or jump anywhere on the current one.
javascript:void(0) Versus href=”#”
A very common alternative to javascript:void(0) is using a plain hash symbol as the href value instead, written as href=”#”. Both approaches aim to create a link that doesn’t navigate to a new page, but they behave slightly differently.
Using href=”#” technically does navigate, specifically to the top of the current page, which can cause an unwanted jump or scroll if the user is partway down a long page when they click the link. javascript:void(0), by contrast, evaluates to undefined immediately and causes no scrolling or navigation behavior at all, making it the more precise choice when the goal is a link that truly does nothing on its own.
Why You Might See It Appear as Visible Text
Occasionally, instead of functioning invisibly in the background, the literal text “javascript:void(0)” appears directly on a webpage, often as an error-like message or unexpected pop-up. This typically happens for one of a few reasons:
JavaScript is disabled in the user’s browser, meaning the onclick handler that was supposed to run never executes, leaving only the inert href value behind; the browser is outdated and handling the javascript: pseudo URL incorrectly; or, in some cases, aggressive pop-up blocking or security extensions are interfering with how the page’s JavaScript executes.
If you encounter this as a regular website visitor, checking whether JavaScript is enabled in your browser settings, and updating your browser to a current version, are the most common fixes.
Is It Still Good Practice in Modern Web Development?
While javascript:void(0) remains functional and widely understood, most modern web development guidance recommends against relying on it as a long-term solution, for a few specific reasons.
From an accessibility and user experience standpoint, a link that doesn’t actually navigate anywhere can be confusing, particularly for users relying on screen readers or other assistive technology, who may expect an anchor tag to behave like a traditional link.
When an interactive element is meant to trigger an action rather than navigate, many developers now recommend using an actual element instead of an anchor tag, since buttons are semantically designed for triggering actions and carry better built-in accessibility support. For cases where a link element truly is appropriate, a more modern and widely recommended approach is to use JavaScript’s event.preventDefault() method within the click handler, rather than relying on javascript:void(0) in the href attribute itself.
This keeps the href pointing to a meaningful, real fallback URL when possible, while still allowing custom JavaScript behavior to override the default navigation when needed.
A Quick Reference Summary
To summarize the core facts: javascript:void(0) combines the javascript: pseudo URL prefix with the void operator applied to the placeholder value 0, producing an expression that executes without any visible effect and returns undefined.
It is most commonly used to prevent a link from navigating or reloading the page, allowing a separate onclick handler to perform the actual desired action instead. While it remains a functional and historically common technique, most current best practices favor using semantic elements for action-triggering controls, or using event.preventDefault() as a more modern, accessibility-friendly alternative when an anchor tag is genuinely the right choice.
Final Thoughts
javascript:void(0) is a small but historically significant piece of JavaScript syntax, one that solved a genuine problem, letting a link trigger custom behavior without navigating away or reloading the page, in a way that became deeply embedded in web development conventions over the years.
Understanding what it actually does, evaluating an expression and discarding the result to return undefined, demystifies what can otherwise look like a strange or even broken piece of code, while also making clear why many developers today reach for more modern, accessible alternatives when building interactive elements.























