If you've ever worked with JavaScript, you may have come across the void(0) expression. It shows up in hyperlinks, pop-up blockers, and old-school JavaScript tricks. For many developers, especially those newer to front-end development, void(0)can seem pretty strange or unnecessary.
Let's talk about what void(0) does in JavaScript, where and why it appears in code, and how to replace or fix it depending on your use case. I'll also answer a few of the most common questions developers ask about void(0).
What is void(0) in JavaScript?
void(0) is an expression that does nothing and returns nothing.
It’s used to prevent the browser from doing its default action, like following a link, without throwing an error or showing any result. void is a unary operator in JavaScript that evaluates the expression you give it (like 0) and returns undefined, discarding the result.
In void(0), the 0 is just a placeholder and has no special meaning. It's commonly used because it’s short and doesn’t cause side effects.
It just evaluates to undefined, which means: “Don’t do anything, and don’t return anything.” More specifically, it returns an undefined primitive value. You know, kind of like the "empty space" of the void.
If you're working with JS, you might see void(0) inside links to prevent page reloads or to stop the user from navigating. You’ll often see it inside an anchor tag like href="javascript:void(0)". That’s a way of saying, “Make this clickable, but don’t go anywhere when clicked.”
Note that this is mostly seen in older code. There are better options now. I'll go into a few of those below. If you're learning JavaScript, these should look familiar.
Why You Might See void(0)
Most likely, you're seeing void(0) because the website is using it to stop a link from doing anything when you click it. Normally, when you click a link, your browser tries to load a new page. But sometimes, a developer wants a link to run some JavaScript instead of going anywhere.
To make that happen, they use void(0) as a trick to cancel the default behavior of the link.
This approach was common in older websites, but it's not the best way to do things anymore. These days, developers use better methods to control what happens when you click something. Here's what the older code looks like.
<a href="javascript:void(0)" onclick="alert('This has been clicked')">CLICK THIS</a>
If void(0) is showing up in your browser or on the screen, it probably means the link is broken, JavaScript isn’t working properly, or the developer forgot to replace that placeholder with real content.
Alternatives to Using void(0)
Modern alternatives to using void(0) tend to be cleaner and more reliable. And that has been true for a while. The most common approach is to use modern JavaScript to stop the link from doing its usual job. Instead of using a fake link that does nothing, developers now write code that listens for clicks and tells the browser not to follow the link.
Here's an example you can try in an online JavaScript editor, which will show you the effect in your browser.
<a href="#" id="myLink">Example Link</a>
<script>
document.getElementById("myLink").addEventListener("click", function(event) {
event.preventDefault(); // stops the link from navigating
console.log("Link clicked, but no navigation happened.");
});
</script>
See the event.preventDefault() line? It's the event handler that stops the link from being followed instead of href="javascript:void(0)".
Another best practice is to use a button instead of a link. If you're not actually sending someone to another page, a button makes more sense. It's easier to work with, better for accessibility, and avoids the need for tricks like void(0).
<button id="myButton">Example Button</button>
<script>
document.getElementById("myButton").addEventListener("click", function() {
console.log("Button clicked. Action performed.");
});
</script>
This approach is better when the element doesn't need to go anywhere. A button is meant for triggering actions, while a link is meant for navigation. Using buttons makes your code more semantic, accessible, and easier to style and maintain.
No tricks, no void(0), and no need to prevent default behavior.
These methods keep the behavior and structure of the page separate, which makes the code easier to maintain and understand. They also prevent confusing behavior like scrolling to the top of the page or showing strange text in the address bar.
And that brings me to some of the common questions around this topic.
What Does Void(0) Actually Do in JavaScript?
void(0) tells the browser to evaluate the number 0, ignore the result, and return undefined. In plain terms, it runs code that does nothing and makes sure nothing is returned or displayed. It’s like a silent placeholder that avoids side effects.
This was useful in situations where developers wanted to attach behavior to something (like a link) without actually triggering a page reload or a navigation event.
Why is void(0) used in links or anchor tags?
You’ll often see void(0) inside a href attribute, like href="javascript:void(0)". That’s an old way of making a link clickable without making it go anywhere.
Developers used it when they wanted to run JavaScript when a link was clicked, but didn’t want the browser to reload the page or jump to the top. It was a workaround to keep the link looking and acting like a link while avoiding its default behavior.
Is void(0) still necessary in modern web development?
No, it’s not necessary anymore. Today’s JavaScript and HTML standards give you better tools to control behavior (and some of those are discussed on the Hackr YouTube channel).
You can use event.preventDefault() to stop a link from navigating, or use a <button> if you’re not linking to another page. These methods are more semantic, easier to read, and more compatible with accessibility tools. void(0) still works, but it’s considered outdated and should be avoided in new code.
What’s the difference between void(0) and href="#"?
Both void(0) and href="#" are used to stop a link from navigating, but they behave a little differently. href="#" makes the browser jump to the top of the page unless you stop it with event.preventDefault(). void(0) doesn’t cause that jump because it evaluates to undefined right away. So while they serve a similar purpose, void(0) avoids that scrolling behavior. Still, neither is ideal compared to using proper event handling.
What should I use instead of void(0) today?
The best modern alternatives are event.preventDefault() and using buttons for actions. If you want a link that doesn't navigate, attach an event listener and prevent its default behavior with JavaScript. If your element is just meant to trigger an action, like opening a menu or running a function, use a <button> instead of a link. It’s cleaner, more accessible, and better aligned with how browsers and users expect things to work.
That's the complete guide to void(0). I hope you found it useful for your JavaScript projects (or for fixing someone else's old code). Let me know if you have any questions in the comments below.