An Introduction to JavaScript Void 0

When referring to plain English, void means an empty space that holds nothing. But when talking about the void keyword in JavaScript or other programming languages, it refers to returning nothing. You might have used it as a return type while writing codes. When you want a function or any statement to return any specific data type, you mention return data_type. However, when you want a method to return nothing, you use return void. JavaScript Void 0 is similar to that.

Java Certification TrainingENROLL NOW
Master Core Java 8 Concepts, Java Servlet, & More!

What Does JavaScript Void 0 Mean?

JavaScript void 0 means returning undefined (void) as a primitive value. You might come across the term “JavaScript:void(0)” while going through HTML documents. It is used to prevent any side effects caused while inserting an expression in a web page. For instance, URLs or hyperlinks are the common examples of using JavaScript void 0. Suppose you insert a link and want to call some JavaScript through it. Usually, when you click on a link, the browser will either reload or open a new page. However, if you just want to call JavaScript through that link, you would not want the entire page to refresh. This is where the JavaScript:void(0) will come in handy.

When you use JavaScript void 0, it will return an undefined primitive value. This will prevent the browser from opening a new or reloading the web page and allowing you to call the JavaScript through it.

Preventing a Page to Reload Using JavaScript Void 0

We can use JavaScript void 0 to prevent reloading of a page. Let’s understand this with the two examples given below. The first example does not use JavaScript:void(0), but the second uses it to prevent reloading. We will also use the ondblclick event handler so that nothing happens on a single click and the action is handled on double click.

<!DOCTYPE html><html><head><title>without using JavaScript:void(0)</title></head><body align="center">    <h2>This is without using JavaScript:void(0)</h2><a href="#" ondblclick="alert('Task completed!')">Double Click Me!</a></body></html>

Output Before Clicking:

JavaScript_void_0_1.

Output After Clicking:

JavaScript_void_0_2.

Color of the Link:

JavaScript_void_0_3

Did you notice that the page refreshed after clicking on the link? If not, there’s another way to get the difference. After clicking on the link, you will see that the color of the link has changed to purple (the default color for visited links). This means that the page has refreshed due to the href tag. On the other hand, in our example below, you will notice that the link still appears blue as only the JavaScript code to alert the message is triggered, but the page is not refreshed.

<!DOCTYPE html><html><head><title>using JavaScript:void(0)</title></head><body align="center">    <h2>Using JavaScript:void(0)</h2><a href="JavaScript:void(0);" ondblclick="alert('Task completed!')">Double Click Me!</a></body></html>

Output Before Clicking:

JavaScript_void_0_4

Output After Clicking:

JavaScript_void_0_5.

JavaScript_void_0_6

Color of the Link:

JavaScript_void_0_4

AI-Powered Full Stack Developer ProgramExplore Program
Want a Top Software Development Job? Start Here!

JavaScript Void 0 Alternatives

Although JavaScript void 0 is an easy solution to prevent a page from reloading, you might sometimes want to use alternatives. Some of these alternatives include:

  • You can use event.preventDefault with the event handler as an alternative to JavaScript:void(0). Here’s an example:
<!DOCTYPE html><html><head><title>JavaScript void 0 alternatives</title></head><body align="center">    <h2>Using .preventDefault()</h2><a href="https://www.simplilearn.com" onclick="event.preventDefault();" ondblclick="alert('Task completed')">Double Click Me!</a></body></html>

Output:

JavaScript_void_0_8

The output here will be the same as that while using JavaScript:void(0). If you do not use the .preventDefault(), the link will take you to Simplilearn’s website.

NOTE: If you have already visited “https://www.simplilearn.com,” the link will appear purple even if you use .preventDefault(). In such a scenario, you can either clear all your browser cookies and history or simply change the link in the <a> tag just to see the changes.

  • Another alternative to JavaScript void 0 is to use return false. When the click returns false, the browser will not take any action.
<!DOCTYPE html><html><head><title>JavaScript void 0 alternatives</title></head><body align="center">    <h2>Using return false()</h2><a href="https://www.simplilearn.com" onclick="return false;" ondblclick="alert('Task completed')">Double Click Me!</a></body></html>

Output:

JavaScript_void_0_9.

With the AI Accelerator ProgramEnroll Now
Design Smarter AI Workflows

Conclusion

Understanding concepts like void 0 helps you write more effective JavaScript code and strengthens your programming fundamentals. To deepen your knowledge, explore Simplilearn's free courses on SkillUp or build a stronger foundation with the Java Certification Training course. The course covers core programming concepts, hands-on development skills, and prepares you for software and web development roles.

Java is a strong place to start, but it's only the beginning of what you can build. If you're ready to move from writing programs to building intelligent ones, Simplilearn's AI Accelerator Program takes you there in 8 weeks, teaching you to build real AI apps and automate workflows with tools like LangChain, n8n, and GitHub Copilot.

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S is a Technical Content Strategist and Data Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More
  • Acknowledgement
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.
  • *All trademarks are the property of their respective owners and their inclusion does not imply endorsement or affiliation.
  • Career Impact Results vary based on experience and numerous factors.