Which one is better to use for a JavaScript link, “#” or “javascript:void(0)”?

Using “javascript:void(0)” is definitely better, since its faster. Try to run both the examples in Google Chrome with the developer tools. The “javascript:void(0)” method takes less time than the only #.

Here’s the usage of “javascript: void(0)”:

If inserting an expression into a web page results in an unwanted effect, then use JavaScript void to remove it. Adding “JavaScript:void(0)”, returns the undefined primitive value.

The void operator is used to evaluate the given expression. After that, it returns undefined. It obtains the undefined primitive value, using void(0).

The void(0) can be used with hyperlinks to obtain the undefined primitive value


Live Demo

<!DOCTYPE html>
      <title>Understanding JavaScript void(0)</title>
      <a href = "javascript:void(0);" ondblclick = "alert('Click it twice!')">
         Click me not once, but twice.</a>

Updated on: 13-Sep-2019


Kickstart Your Career

Get certified by completing the course

Get Started