0% found this document useful (0 votes)
24 views

What Is JavaScript Void Keyword

The void keyword in JavaScript can be used as a unary operator before an expression to evaluate the expression without returning a value. It is commonly used in JavaScript URLs to evaluate an expression for its side effects without loading the result into the document. Some examples show how void can be used to generate the undefined value or evaluate an expression that has no effect.

Uploaded by

Pening Lalat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

What Is JavaScript Void Keyword

The void keyword in JavaScript can be used as a unary operator before an expression to evaluate the expression without returning a value. It is commonly used in JavaScript URLs to evaluate an expression for its side effects without loading the result into the document. Some examples show how void can be used to generate the undefined value or evaluate an expression that has no effect.

Uploaded by

Pening Lalat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

What Is

JavaScript
Void Keyword?

Computer Programming & IT Books | ITBooksHub.com


http://freepdf-books.com
The 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. Its syntax could 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 for this operator is in a client-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:
<head>
<script type="text/javascript">
<!--
//-->
</script>
</head>
<body>
<a href="javascript:void(alert('Warning!!!'))">Click
me!</a>
</body>

Example 2:
Another 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:
<head>

Computer Programming & IT Books | ITBooksHub.com


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

Example 3:
Another use for void is to purposely generate the undefined value as
follows:
<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>

Computer Programming & IT Books | ITBooksHub.com


http://freepdf-books.com

You might also like