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!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
What is
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 returnundefined
when an anchor tag is clicked, while you are on the page that thehref
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 returnundefined
. That way nothing will happen and the page will not be refreshed.Here is how the tag usually looks like:
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 tojavascript:void(0)
and one that we leave the value empty as follows:Now if you were to click on
Link 1
nothing will happen, the page will not be refreshed. But if you were to click onLink 2
your page will get refreshed.As a next step let’s break the statement down a little bit.
What exactly is
javascript:
?The
javascript:
part is a pseudo-URL address. So when a browser follows ajavascript:
, 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 returnundefined
, we have to add thevoid
operator.Next, let’s review what the
void
operator is.What exactly is the
void
operator?The
void
operator checks the given value and then returnsundefined
. In most cases, it is used mostly to obtain the valueundefined
by usingvoid(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 tojavascript:void(0)
, you can add the#
as the value to yourhref
tags like this: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 addonclick="return false;"
so that the user does not get taken to the top of the page:With that approach, you would have the same behavior as you would with
javascript:void(0)
.Conclusion
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 theonclick="return false;"
approach as an alternative which in some cases might be a bit more readable than using thejavascript: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