What does "javascript: void (0)" mean in href of tag a?

Question:

What does the value javascript:void(0) in the fields href tags a in HTML ?

Example:

<a href="javascript:void(0)" id="btnClick">Click</a>

Answer:

The void operator evaluates the given expression and returns undefined

The reason for using this expression in a link href is because this attribute causes a redirect to a plain text version of what is obtained from the function. But if the result is undefined the redirection does not occur. This is the shortest way to not redirect and do nothing on a link .

Differences with other methods to not redirect:

  • href="" Reload the current page

  • href="#" Scrolls to the top of the page

  • href="javascript: void(0)" Does nothing

  • href="javascript:;" It does nothing, but it does not work in all browsers, for example, in IE7 it redirects to a new window And also, it is not as accepted as a de facto standard than the previous one

More info:

Scroll to Top