Skip to content

JavaScript void operator

  • by

The JavaScript void operator evaluates the given expression and then returns a value of undefined. This operator specifies an expression to be evaluated without returning a value. Its syntax could be one of the following.

void expression;
void(expression);

JavaScript void operator

Simple example code When a browser follows a javascript: URI, it evaluates the code in the URI and then replaces the contents of the page with the returned value, unless the returned value is undefined. The void the operator can be used to return undefined.

<!DOCTYPE html>
<html>
<body>
  <a href="javascript:void(0);">
    Click here to do nothing
  </a>

  <br>
  <a href="javascript:void(document.body.style.backgroundColor='gray');">
    Click to change background
  </a>

</body>
</html>

Output:

JavaScript void operator

In the following web document, the void operator is used to call a function.

<!DOCTYPE html>
<html lang="en">
<body>
  
  <a href="javascript:void(testfunc())">
  Click here to call testfunc()</a>

</body>
</html>

Do comment if you have any doubts or suggestions on this Js operator topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading