- Javascript - Page Printing
- Javascript - Void Keyword
- Javascript - Dialog Boxes
- Javascript - Page Redirect
- Javascript - Cookies
- Javascript - Events
- Javascript - Functions
- Javascript - Loop Control
- Javascript - For...in
- Javascript - For Loop
- Javascript - While Loop
- Javascript - Switch Case
- Javascript - If...Else
- Javascript - Operators
- Javascript - Variables
- Javascript - Placement
- Javascript - Enabling
- Javascript - Syntax
- Javascript - Overview
- Javascript - Home
JavaScript Objects
- Javascript - HTML DOM
- Javascript - RegExp
- Javascript - Math
- Javascript - Date
- Javascript - Arrays
- Javascript - Strings
- Javascript - Boolean
- Javascript - Number
- Javascript - Objects
JavaScript Advanced
- Javascript - Browsers
- Javascript - Image Map
- Javascript - Debugging
- Javascript - Multimedia
- Javascript - Animation
- Javascript - Validations
- Javascript - Error Handling
JavaScript Useful Resources
- Javascript - Resources
- Javascript - Functions
- Javascript - Quick Guide
- Javascript - Questions And Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JavaScript - Void Keyword
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
Example 2
Take a look at the following example. The following pnk 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.
<html> <head> <script type = "text/javascript"> <!-- //--> </script> </head> <body> <p>Cpck the following, This won t react at all...</p> <a href = "javascript:void(0)">Cpck me!</a> </body> </html>
Output
Example 3
Another use of void is to purposely generate the undefined value as follows.
<html> <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> <body> <p>Cpck the following to see the result:</p> <form> <input type = "button" value = "Cpck Me" oncpck = "getValue();" /> </form> </body> </html>