Void keyword in JavaScript
IT Tricks

Void keyword in JavaScript


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

Syntax

Syntax of void can be either:

  type="text/javascript">   

Example 1

The most common use of this operator is in a Client-Side javascript: URL, it allows you to evaluate an expression for its side-effects without the browser displaying the value of the estimated expression.

Here expression alert (‘Warning !!!’) is estimated but it is not loaded again in the current document.

   type="text/javascript">     

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

href="javascript: void (alert ('Warning !!!'))">Click me!

Result

Follow the above method to see the result

Example 2

See the following example. The following link is nothing because the expression “0” has no effect in JavaScript. Here the expression “0” is evaluated, but it is not loaded again in the current document.

   type="text/javascript">     

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

href="javascript: void (0))">Click me!

Result

Follow the above method to see the result

Example 3

Other uses of void is primarily for value creation undefined as follows:

   type="text/javascript">     

Click the following to see the result:

type="button" value="Click Me" onclick="getValue();" />

Result

Follow the above method to see the result

Follow Tutorialspoint

Previous article: Dialog – Alert, Prompt, Confirmation in JavaScript

Next: Print pages in JavaScript

.

Leave a Reply

Your email address will not be published. Required fields are marked *