3 Example(s) of JavaScript void operator


Description :

VoidOperator operator return undefined after evaluating it.


JavaScript void operator Example - 1
<!DOCTYPE html>
<html>
 <head>
    <title>Welcome to LearnKode - A code learning platform</title>
</head>
  <body>
      <p>Click the following, This won't react at all...</p>
      <a href="javascript:void(alert('Warning!!!'))">Click me!</a>

      </body>
</html>

Output

Description :

It means it’ll do nothing, this void function will prevent the default behaviour of anchor tag.


JavaScript void operator Example - 2
<!DOCTYPE html>
<html>
 <head>
    <title>Welcome to LearnKode - A code learning platform</title>
</head>
  <body>
     <a href="javascript:void(0);">
  Click here to do nothing
</a>
      </body>
</html>

Output

Description :

It will change the background color of body on clicking of anchor button.


JavaScript void operator Example - 3
<!DOCTYPE html>
<html>
 <head>
    <title>Welcome to LearnKode - A code learning platform</title>
</head>
  <body>
  <a href="javascript:void(document.body.style.backgroundColor='green');">
  Click here for green background
</a>
      </body>
</html>

Output