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

JavascriptWeb DevelopmentFront End Technology

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>
raja
Published on 29-Jan-2018 15:22:54
Advertisements