Home >Web Front-end >JS Tutorial >Detailed explanation of the use of void statement in JavaScript_Basic knowledge

Detailed explanation of the use of void statement in JavaScript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 15:56:481323browse

void is an important keyword in JavaScript that can be used as a unary operator before its single operand, which can be of any type.

This operator specifies an expression that does not require a return value and is evaluated. Its syntax may be one of the following:

<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 operation is in client-side JavaScript: URLs, which can evaluate an expression that computes its boundary effects without displaying the value of the computed expression to the browser.

Here, the expression alert('Warning!!!') evaluates but does not load back the current document:

<head>
<script type="text/javascript">
<!--
//-->
</script>
</head>
<body>
<a href="javascript:void(alert('Warning!!!'))">Click me!</a>
</body>


Example 2:

Another example the link below 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:

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


Example 3:

Another use of void is to intentionally produce undefined values, like this:

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn