Void keywords 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 defines an expression to be evaluated without returning a value.

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 defines an expression to be evaluated without returning a value.

Syntax

The syntax of void can be one of two types:

  type = "text/javascript" >  

Example 1

The most common use of this operator is in a Client-Side javascript : URL, which allows you to estimate an expression for its secondary effects (side-effects) that the browser does not display value. of the estimated expression.

Here the alert ('Warning !!!') expression is evaluated but it is not loaded back 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 results

Example 2

See the following example. The following link will be nothing because the expression "0" has no effect in JavaScript. Here the expression "0" is estimated, but it is not loaded back 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 results

Example 3

Another use of void is primarily to create undefined values ​​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 results

According to Tutorialspoint

Previous article: Dialogs - Alert, Prompt, Confirmation in JavaScript

Next lesson: Print the page in JavaScript

5 ★ | 1 Vote