JavaScript

What Does Javascript:void(0) Mean?

In JavaScript, any expression/statement that is evaluated with the void keyword will return an undefined value. An operand 0 can be used with the void keyword which tells the browser to “do nothing”. Maybe you have seen “javascript:void(0)” in any html document. But did you know What exactly javascript:void(0) is? If no! Then this write-up is going to help you in this regard.

This article will provide a brief overview of the following concepts:

So, let’s get started!

What is javascript:void(0) in JavaScript?

The “javascript:void(0)” is a combination of two things i.e., “javascript:” and “void(0)”. The below-listed points will assist you in understanding what exactly “javascript:void(0)” is?

  • The term “javascript:” can be stated as a Pseudo Uniform Resource Locator (URL).
  • Void is a unary operator in JavaScript that returns undefined.
  • If we talk about the void(0), it directs the browser to “do nothing”.
  • There are two implementations of 0 operator i.e. void(0) and void 0.

Working of javascript:void(0)

Combinedly the term “javascript:void(0)” works as follows:

The “javascript:” allows us to run a code without changing the current page while void(0) directs the browser to “do nothing”, do not reload/refresh the page, don’t run the code. So, all in all the “javascript:void(0)” stops the browser from refreshing, navigating or reloading a page.

Practical implementation of javascript:void(0)

Let’s consider some examples to understand the working of javascript:void(0).

Example: basic understanding of void(0)

In this example, we will pass a link “linuxhint.com” and javascript:void(0) to the href attribute of the <a> tag:

1
<a href="javascript:void(0);https://www.linuxhint.com/"> Click on the link to understand the working of void 0 </a>

In this program we defined a link, however we utilized the void(0) therefore Clicking on the link wouldn’t perform any action:

The output verified the working of javascript:void(0).

Example: a comparative example

In this program, we will create two links, one with void(0) and the second one without void(0):

1
2
3
<a href="javascript:void(0);https://www.linuxhint.com/">Link with void(0)</a>
<br>
<a href="https://www.linuxhint.com/">Link without void(0)</a>

The below given gif will assist you to understand the working of void(0) in a better way:

The output verified that the link with void(0) didn’t perform any task while the link without void(0) directed us to the specified link.

This is how the javascript:void(0) prevents a page from reloading/navigating/refreshing.

Conclusion

The “javascript:” allows us to run a code without changing the current page while void(0) directs the browser to “do nothing”, do not reload/refresh the page, don’t run the code. So, combinedly, we can say that the “javascript:void(0)” stops the browser from refreshing, navigating or reloading a page. This write-up explained what does javascript:void(0) mean using a couple of examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.