Question
In HTML, I found this link:
<a href="javascript:void(0)" id="loginlink">login</a>
I have seen href="javascript:void(0)" used many times, but I do not understand exactly what it means, how it works, or why developers use it in anchor tags.
Short Answer
By the end of this page, you will understand what javascript:void(0) does, why it has been used inside href attributes, what the void operator returns, and why modern code usually prefers better alternatives such as buttons or event listeners that prevent default link behavior.
Concept
javascript:void(0) is an old JavaScript pattern often placed inside an anchor tag's href attribute.
<a href="javascript:void(0)">login</a>
To understand it, break it into two parts:
javascript:is a special URL scheme that tells the browser to execute JavaScript code instead of navigating to a normal web address.void(0)runs the JavaScript expression0, then forces the result to beundefined.
So this:
href="javascript:void(0)"
means:
- when the link is clicked,
- execute a tiny bit of JavaScript,
- return nothing useful,
- and do not navigate to another page.
Why developers used it
Developers often used it when they wanted an <a> element to look like a link or be clickable, but they did not want it to actually go anywhere. They would then attach a click handler with JavaScript.
Example:
Mental Model
Think of an anchor tag as a door with an address written on it.
- A normal link like
/loginsays: "Go to this place when clicked." javascript:says: "Instead of going somewhere, run this instruction."void(0)says: "Run it, but produce no meaningful result."
So javascript:void(0) is like putting a fake destination on the door that tells the browser, "Do not travel anywhere; just stay here."
That is why it was often used for clickable UI elements that were not really navigation links.
Syntax and Examples
The basic pattern looks like this:
<a href="javascript:void(0)">Click me</a>
What void does
In JavaScript, void evaluates an expression and always returns undefined.
console.log(void 0); // undefined
console.log(void(123)); // undefined
console.log(void 'test'); // undefined
0 is not special here because of its value. It is just a simple expression. These all behave similarly:
void 0;
void(0);
void 99;
();
Step by Step Execution
Consider this example:
<a href="javascript:void(0)" id="loginlink">login</a>
<script>
document.getElementById('loginlink').addEventListener('click', function () {
console.log('Login clicked');
});
</script>
Here is what happens step by step when the user clicks the link:
- The browser detects a click on the
<a>element. - The click event listener attached with
addEventListenerruns. console.log('Login clicked')prints the message.- The browser also checks the
hrefattribute. - Because the
hrefstarts withjavascript:, the browser treats the rest as JavaScript code. - It evaluates
void(0).
Real World Use Cases
Although javascript:void(0) is less common in new code, you may still encounter it in real situations.
1. Legacy web applications
Older dashboards and admin panels often used it for menu items, modal triggers, and expandable sections.
<a href="javascript:void(0)" onclick="openMenu()">Menu</a>
2. Placeholder links during development
Sometimes developers used it temporarily before adding a real destination.
<a href="javascript:void(0)">Coming soon</a>
This works, but a plain button or disabled UI is usually better.
3. JavaScript-controlled UI actions
Links were often used to trigger:
- login popups,
- tab switching,
- accordion toggles,
- dropdown menus,
- client-side widgets.
4. Reading older libraries or templates
If you work with old jQuery plugins, CMS themes, or inherited codebases, you may see many anchors using this pattern.
Understanding it helps you safely refactor those parts into more modern code.
Real Codebase Usage
In real projects today, developers usually avoid putting JavaScript directly in href. Instead, they separate structure, behavior, and meaning.
Common modern patterns
Use a button for actions
If the click performs an action instead of navigation:
<button id="loginButton">login</button>
document.getElementById('loginButton').addEventListener('click', function () {
openLoginModal();
});
This is the most semantic pattern.
Use a real link plus preventDefault
If the element is conceptually a link, but JavaScript may intercept it:
<a href="/login" id="loginLink">login</a>
.().(, () {
event.();
();
});
Common Mistakes
1. Using a link for something that is not navigation
Broken idea:
<a href="javascript:void(0)" id="saveBtn">Save</a>
Why it is a problem:
- saving is an action, not navigation;
- a
<button>is more appropriate.
Better:
<button id="saveBtn">Save</button>
2. Thinking void(0) means “do nothing” everywhere
It does not stop all JavaScript. It only evaluates an expression and returns undefined.
console.log(void 0); // undefined
It does not cancel events by itself.
3. Mixing behavior into HTML
Older style:
Comparisons
| Approach | What it does | Good for | Drawbacks |
|---|---|---|---|
href="javascript:void(0)" | Executes JS and returns undefined | Reading or maintaining legacy code | Outdated, mixes JS into HTML |
href="#" | Points to page fragment top unless prevented | Quick prototypes | Can cause page jump |
<button> | Triggers an action | Forms, dialogs, toggles, actions | Not for actual navigation |
<a href="/path"> | Navigates to another page or route | Real links | Needs JS interception if behavior changes |
<a href="/path" + |
Cheat Sheet
Quick reference
Meaning
href="javascript:void(0)"
javascript:= run JavaScript from the URLvoid(0)= evaluate0, then returnundefined- result: no useful navigation target
void operator
void 0; // undefined
void(123); // undefined
void 'text'; // undefined
Why it was used
- make a link clickable without navigating
- attach behavior with JavaScript
- common in older code
Why it is usually avoided now
- mixes JavaScript into HTML
- weaker semantics
- less maintainable
- accessibility concerns
Better choices
For actions:
<>Open
FAQ
What does javascript:void(0) do in HTML?
It tells the browser to execute JavaScript instead of following a normal URL. The void(0) expression returns undefined, so the link does not navigate anywhere useful.
Why is void(0) used instead of just 0?
Because void forces the result to be undefined. The goal is to avoid returning a value that might be treated differently by the browser.
Is javascript:void(0) the same as return false?
No. void(0) simply evaluates to undefined. return false is used in certain event-handling contexts to stop default behavior, depending on how the handler is written.
Is javascript:void(0) bad practice?
In modern code, it is usually not the best option. It is mostly a legacy pattern. Prefer buttons for actions and real links for navigation.
Can I use href="#" instead?
You can, but clicking it may jump the page to the top unless you call . It is usually better to use a button for actions.
Mini Project
Description
Build a small login trigger component that demonstrates the difference between using a legacy clickable link pattern and a modern semantic approach. The project shows how to open a login message without navigating away from the page.
Goal
Create a clickable login control that opens a message on the page using modern event handling instead of relying on javascript:void(0).
Requirements
- Create a visible login control on the page.
- Show a message such as "Login dialog opened" when the control is clicked.
- Do not navigate to another page when the control is used.
- Use JavaScript with
addEventListener. - Use a semantic element appropriate for an action.
Keep learning
Related questions
Adding Table Rows with jQuery: append(), Limits, and Best Practices
Learn how to add table rows in jQuery using append(), what elements are allowed in tables, and safer ways to build rows dynamically.
Bower vs npm: What’s the Difference in JavaScript Package Management?
Learn the plain difference between Bower and npm, how each manages packages, and why npm replaced Bower in most JavaScript projects.
Can `(a == 1 && a == 2 && a == 3)` Ever Be True in JavaScript?
Learn how JavaScript type coercion, loose equality, and custom object conversion can make `a == 1 && a == 2 && a == 3` true.