Why use void(0) in JavaScript?

The void operator evaluates the given expression and then returns undefined. If you have pass 0 as the unary expression operand to the void operator, JavaScript coerces 0 to "false" and returns, but void doesn't care and simply returns undefined, which means "do nothing" . Put them together and you have composed a way to programmatically "do nothing" when a link is clicked. JavaScript Void(0) is often used when, inserting an expression into a web page may produce an unwanted side-effect. You may occasionally encounter an HTML document that uses href="JavaScript:Void(0);" within an < a > element.
<a href="javascript:void(0)" id="doIt">doSomething</a>
A common usage of JavaScript:Void(0) is with hyperlinks. Usually, when the user clicks a link on a page a new page loads, but this is not always the desired course of action. For example, when the user clicks a link on a page, you might want to update the value of a field in a form or the value of a variable. To prevent a page from reloading when a link is clicked, the void(0) function is used. example
<a href="#" onclick="alert('I am reloading!!!')">Click me!</a>
output
run this source code Browser View
Click me!

When you click on the above link. Even though the code within it specifies to display a message in an alert box (and this action does take place), the page will reload automatically.

To prevent the page reloading, we can use JavaScript:void(0); within the anchor link.
<a href="javascript:void(0);" onclick="alert('Not reloaded!!!')">Click me!</a>
output
run this source code Browser View
Click me!
When you run the above example, you can see a message is displayed in an alert box as intended and void(0) prevents the page from reloading . So the void operator can be useful when you need to call another function that may have otherwise resulted in an unwanted page refresh.