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

Example

Live Demo

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

Updated on: 13-Sep-2019

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements