What does “javascript:void(0)” mean?

JavascriptWeb DevelopmentFront End Technology

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>

We used JavaScript: void(0) above to prevent the page from reloading when the button is clicked the first time.

As shown above, the code will only work when the button will be clicked twice. If it is clicked once, then nothing happens. But the alert box comes over when the button is clicked twice since we used the ondblclick event handler.

raja
Ali
Published on 17-Jan-2018 16:38:26
Advertisements