English 中文(简体)
Javascript - Void Keyword
  • 时间:2024-11-03

JavaScript - Void Keyword


Previous Page Next Page  

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>

Example 1

The most common use of this operator is in a cpent-side javascript: URL, where it allows you to evaluate an expression for its 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>Cpck the following, This won t react at all...</p>
      <a href = "javascript:void(alert( Warning!!! ))">Cpck me!</a>     
   </body>
</html>

Output