By Bobby Iliev
Hi all,
I wanted to create this question/answer about a topic that I’ve been recently asked a few times: What does Javascript void(0); mean?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
javascript:void(0)
?If you have done any web development, you have most likely seen javascript:void(0)
in some HTML files that you were working on.
What javascript:void(0)
is most commonly used for is to return undefined
when an anchor tag is clicked, while you are on the page that the href
attribute value is pointing to.
What this means is that if you press, for example, the Home
page of a website, while you are on the home page of that specific website, JavaScript will return undefined
. That way nothing will happen and the page will not be refreshed.
Here is how the tag usually looks like:
<a href="javascript:void(0)">Home</a>
If a user clicks on the above button, instead of the browser loading a brand new page, nothing will happen. This provides a better user experience.
To see the exact difference, we could create one anchor tag and set the href
value to javascript:void(0)
and one that we leave the value empty as follows:
<div>
<a href="javascript:void(0)">Link 1</a>
</div>
<div>
<a href="">Link 2</a>
</div>
Now if you were to click on Link 1
nothing will happen, the page will not be refreshed. But if you were to click on Link 2
your page will get refreshed.
As a next step let’s break the statement down a little bit.
javascript:
?The javascript:
part is a pseudo-URL address. So when a browser follows a javascript:
, it then loads the contents of the page with the returned value. But what if the returned value is false? In that case, it does nothing. And if we want it to return undefined
, we have to add the void
operator.
Next, let’s review what the void
operator is.
void
operator?The void
operator checks the given value and then returns undefined
. In most cases, it is used mostly to obtain the value undefined
by using void(0)
.
javascript:void(0)
alternativeAs using the javascript:
pseudo-URL address could cause some problems when a user tries to bookmark a specific link. As an alternative to javascript:void(0)
, you can add the #
as the value to your href
tags like this:
<a href="#">Home</a>
The only difference is that this will take you to the top of the page, while javascript:void(0)
won’t move you at all. To prevent this behavior you could add onclick="return false;"
so that the user does not get taken to the top of the page:
<a href="#" onclick="return false;">Home</a>
With that approach, you would have the same behavior as you would with javascript:void(0)
.
Using javascript:void(0)
will ensure that your users do not navigate to another page and also that their current page does not get reloaded. You could also use the onclick="return false;"
approach as an alternative which in some cases might be a bit more readable than using the javascript:void(0)
approach.
As a next step, try to use these methods a few times to see how they actually work and pick the one that suits your needs.
I hope that this post has helped you! Best, Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.