Main Page

Prefix increment/decrement

If you place this line of code into an HTML page, and click the link, you see
printed on the
screen (Figure 2-3). This occurs because
returns a reference to the newly opened win-
dow (this and other methods of the window are discussed further in Chapter 5, “JavaScript in the
Browser”). That object is then converted to a string for display.
Figure 2-3
To avoid this, use the
call with the void operator:
<a href=”javascript:void(‘about:blank’))”>Click Me</a>
This makes the
call return
, which is not a valid value and is not displayed
in the browser window. Remember, functions that have no return value actually return
Prefix increment/decrement
Two operators taken directly from C (and Java) are prefix increment and prefix decrement. Prefix
increment, which adds one to a number value, is indicated by placing two plus signs (++) in front of a
var iNum = 10;
The second line increments
. This is effectively equal to:
var iNum = 10;
iNum = iNum + 1;
Likewise, the prefix decrement subtracts one from a value. The prefix decrement is indicated by two
minus signs (– –) placed before the variable:
var iNum = 10;
In this example, the second line decreases the value of
When you use prefix operators, note that the increment/decrement takes place
the expression is
evaluated. Consider the following example:
Chapter 2
05_579088 ch02.qxd 3/28/05 11:35 AM Page 34

JavaScript EditorFree JavaScript Editor     Ajax Editor