Which “href” Value Should You Use for JavaScript Links: # or javascript:void(0)?
When you’re creating a link whose sole purpose is to run JavaScript code, you may wonder which href
value is best to use. Should you use #
or javascript:void(0)
? While both options will technically work, there are important differences in functionality, page behavior, and best practices.
Let’s explore each option:
1. Using href="#"
<a href="#" onclick="myJsFunc();">Run JavaScript Code</a>
Here, the href="#"
sets up a clickable link that triggers the JavaScript function myJsFunc()
when clicked. However, there are some drawbacks:
Labels: Which “href” Value Should You Use for JavaScript Links: # or javascript:void(0)?