Tuesday, February 4, 2020

Step by step tutorial guide which explains how to implement JavaScript void keyword with examples


Hello everyone, afternoon to you all and welcome to a new tutorial section on JavaScript. In our tutorial post we talked about JavaScript dialog boxes and I believe by now you can easily create the various dialog boxes without much stress.

In this tutorial post we are going to be discussing about JavaScript Void Keyword. 

Void is an important keyword in JavaScript which can be used as a unary operator that appears before its single operand, which may be of any type. This operator specifies an expression to be evaluated without returning a value.

Syntax

The syntax of void can be either of the following two:

<head>
     <script  type = "text/Javascript">
          <!--
               void  func()
               javascript:void func()
               or:
               void(func())
               javascript:void(func())
          //-->
     </script>
</head>

You can also read our tutorial post on: JavaScript Syntax


Example 1

The most common use of this operator is in a client-side Javascript: URL, where it allows you to evaluate an expression for it's side-effects without the browser displaying the value of the evaluated expression.

Here the expression alert('Warning!!!') is evaluated but it is not loaded back into the current document:

<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>

Output

Below is the output of the above example

Click the following, this won't react at all...

Click Me

You can also read our tutorial post on: JavaScript - while Loop


Example 2

Take a look at the following example. The following link does nothing because the expression "0" has no effect in JavaScript. Here the expression "0" is evaluated,  but it is not loaded back into the current document.

<html>
     <head>
          <script  type = "text/Javascript">
               <!--
               //-->
          </script>
     </head>

     <body>
          <p>Click the following, this won't react at all...</p>
          <a href  =  "javascript:void(0)">Click Me</a>         
     </body>
</html>

Output
Below is the output of the above example 

Click the following, this won't react at all...

Click Me

You can also read our tutorial post on: JavaScript - Loop Control

Example 3
Another use of void is to purposely generate the undefined value as follows:

<html>
     <head>
          <script  type = "text/javascript">
               <!--
                    function  getValue()  {
                         var  a, b, c; 

                         a = void ( b = 5, c = 7 );
                         document.write( ' a = ' + a + ' b = ' + b + ' c = ' + c );   
                    }    
               //-->
          </script>
     </head>

     <body> 
          <p>Click the following to see the result.</p>
          <form>
               <input  type = "button"  value = "ClickMe"  onclick = "getValue();" />    
          </form>
     </body>
</html>

Output
Below is the output of the above example

Click the following to see the result. 

 

Alright guys, we have come to the end of this tutorial post on JavaScript void Keyword. In our next tutorial post we will be discussing about JavaScript Page Printing. 

Like i always tell you guys, always feel free to ask your questions via the comment box, they will be attended to as soon as possible.

Follow us on our various social media platforms in other to stay updated with our latest tutorials. Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment