Why are use the void keyword in JS?

Asked 19-Jul-2021
Viewed 198 times

1 Answer


0

The void is an important keyword in JavaScript. which can be used as an operator. which may be any type of data for access. It returns undefined values. 

Syntax

<head>
   <script type = 'text/javascript'>
         void func(){
             javascript:void func()
         } // or:
         void(func())
             javascript:void(func())
   </script>
</head>

The most common use of void is in client-side JavaScript URLs. we can not change the page after clicking the link then we can add these void keywords on the click event. 

<html>

   <head>
      <script type = 'text/javascript'>

      </script>
   </head>
   <body>
      <p>Click the following, This won't react at all...</p>
      <a href = 'javascript:void(alert('Warning !!!'))'>Click me!</a>
   </body>
</html>

<html>

   <head>
      <script type = 'text/javascript'>
      </script>
   </head>
   <body>
      <a href = 'javascript:void(0)'>Click me !!!</a>
   </body>
</html>