A comprehensive explanation about using javascript:void(0) in javascript. When to use javascript:void(0) and how it works with examples of using it with anchor tag.
Anuj Sharma
Last Updated Jan 5, 2025
javascript:void(0) is a very frequently asked javascript interview question for beginners. It's important to understand URI schemes and void() operator in javascript. In this quick blog, we will cover the details about javascript:
URI scheme, void()
operator, and their usage with examples along with the use-cases of using javascript:void(0) in the web application
javascript:void(0)
is used to add a fake navigation to the HTML element. It can be used as part of HTML elements which support navigation targets with href
property. When the navigation target (href) is invoked on click. It executes the inline javascript code void(0)
which returns undefined
. It means this code doesn't do anything but prevent navigation.
Example: javascript:void(0)
is generally used to prevent navigation when clicking on the anchor tag, and avoid any refresh of the page.
<a href="https://developer.mozilla.org/en-US/docs/Web/URI/Schemes/javascript">
Navigate to javascript: URI scheme !!
</a>
<a href="javascript:void(0)">
Do nothing !!
</a>
JavaScript URLs i.e. javascript:
is one of the types of URI schemes which decides the type of protocol used while executing the resources. Some other well-known protocol examples include mailto:<mail id>
or tel:9675634298
javascript:
is generally used to fake the navigation targets, where rather than having an actual URL, it executes a <JS script>. This script can be any legitimate javascript code which will be executed when this navigation target gets triggered.
For example,
// javascript: followed by inline javascript
<a href="javascript:<script>" >
Execute script on click
</a>
Executing alert box on click
<a href="javascript:alert('Hi FrontendGeek !!')">
Hi FrontendGeek !!
</a>
Executes console.log on click
<a href="javascript:console.log('button clicked !!')">
Hi FrontendGeek !!
</a>
// console: button clicked !!
void(0)
and its usagevoid is an operator which executes the expression and returns undefined
every time, the input expression doesn't matter in this case. An expression can be any valid javascript expression, It always returns undefined.
When void(0)
or void 0
is used, it executes the expression and returns undefined, and due to this, there is nothing happens when clicking for navigation.
// void(0) or void 0 returns "undefined"
href="javascript:void(0)"
or
href="javascript:void 0"
javascript:void(0) usage is not recommended in web development, since it executes the javascript script inline along with the HTML code itself It can lead to bugs if not properly used. It also introduced a few limitations to the web apps, here are a few examples
javascript:void(0)
leads to poor accessibility support, these inline javascript executions as part of href are not well supported by screen readers.There are other ways as well to prevent link navigation that can be used instead of javascript:void(0), since this is not a recommended way of preventing the URL navigation
Event's preventDefault()
method can be used to prevent the default navigation behaviour of button or anchor tags. It requires some additional javascript setup to register the event listener against the button click event.
When the event is triggered for the navigation, the preventDefault()
method suppresses the default behaviour of the HTML elements like refresh on link click or Form submission. here is an example
<a id="greet" href="">
Hi FrontendGeek !!
</a>
preventDefault()
const link = document.getElementById('#greet');
link.addEventListener('click', (event) => {
event.preventDefault();
console.log('Hello clicked !!');
})
Anuj Sharma
Last Updated Jan 4, 2025
Explore the most common ways to reverse a string in javascript including the most optimal way for frontend interviews with O(1) time complexity.
Anuj Sharma
Last Updated Jan 2, 2025
Understand important web authorization techniques to enhance role-based authentication for any web application with popular techniques like Session & JSON Web Token (JWT)
Vivek Chavan
Last Updated Dec 23, 2024
You will get a clear understanding about working with any rest api and common concepts asked during interviews
Anuj Sharma
Last Updated Jan 9, 2025
Go through different ways to display dates using javascript date object. It covers examples of date object usage to understand the main concepts of javascript date object.
Anuj Sharma
Last Updated Dec 27, 2024
An Interview-focused explanation of Promise Polyfill in JavaScript which helps to understand both Functional and ES6 custom promise implementation.
Anuj Sharma
Last Updated Dec 10, 2024
A brief explanation of Cross-Origin Resource Sharing (CORS) concept to enable client application accessing resources from cross domain and HTTP headers involved to enable resource access.
© 2024 FrontendGeek. All rights reserved