What Does JavaScript Void(0) Mean? – POFTUT

What Does JavaScript Void(0) Mean?


JavaScript provides the operator void in order to return undefined results as a result of a function. void also used to evaluate and return undefined result for an expression.

Void Operator

In the general void is used to specify the function return type as nothing or undefined where the function really returns nothing. This function will only execute some statements and evaluate expressions but return nothing.

void function log_message() 
{
  console.log("Executed!"); 
}();

Void(0) Use Cases

void(0) can be used as javascript: URI, where the URI will be evaluated and resulting value or content, will be printed on the page. For example, the following <a> tag will contain This is content .

<a href="javascript:void(0);">
  This is content 
</a>

javascript:void(0) Example

In the following example, we will put javascript:void(0) in anchor tag which will prevent the page reload when it is clicked.

<html>
   <head>
      <title>JavaScript:void(0) Example</title>
   </head>
   <body>
      <center>
         <h1 style="color:green">Poftut.com</h1>
         <h3>JavaScript:void(0)</h3>
         <a href="javascript:void(0);"
            ondblclick="alert('Welcome to Our Page')">
         Double click on me </a>
      </center>
   </body>
</html>

Leave a Comment