spacer
Yehuda Shiran November 20, 1999
The void Operator
Tips: November 1999

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

The void operator specifies an expression to be evaluated without returning a value. Its syntax may be similar to one of the following:

void foo()
javascript:void foo()

or:

void(foo())
javascript:void(foo())

Let's examine the following example:

function foo() {
  alert("Function Entered");
  return true;
}
alert(foo());

The preceding script segment displays two alert boxes. The first box displays the string "Function Entered", as expected. The second box displays what the function foo() returns, the Boolean true. Now, let's look at a slightly different foo():

function foo() {
  alert("Function Entered");
  return true;
}

alert(void foo());

This script also generates two alerts, but the second one reads "undefined", because the void operator evaluates the function without returning a value.

A more important use of this operator is in hypertext links, where it is used to evaluate a JavaScript expression in line. The expression is evaluated but it is not loaded back into the current document. The following link does nothing because the expression "0" has no effect in JavaScript:

<A HREF="javascript:void(0)">Click here to do nothing</A>

The following link generates an alert box when the link is clicked:

<A HREF="javascript:void(alert('Wow'))">Click here to display a message</A>

The parentheses are optional. We chose to use them in order to make the script more readable.


People who read this tip also read these tips:

Look for similar tips by subject:


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger