↑

Main Page

# Multiplicative operators

Just like logical AND, if either operand is not a Boolean, logical OR will not always return a Boolean

value:

?

If one operand is an object and one is a Boolean, the object is returned.

?

If both operands are objects, the first operand is returned.

?

If both operands are

null

,

null

is returned.

?

If either operand is

NaN

,

NaN

is returned.

?

If either operand is undefined, an error occurs.

Also like the logical AND operator, the logical OR operator is short-circuited. In this case, if the first

operand evaluates to

true

, the second operand is not evaluated. For example:

var bTrue = true;

var bResult = (bTrue || bUnknown);

alert(bResult); //outputs “true”

As with the previous example, the variable

c

is undefined. However, because the variable

bTrue

is set

to

true

, variable

bUnknown

is never evaluated and thus the output is

“true”

. If the value of

bTrue

is

changed to

false

, an error occurs:

var bFalse = false;

var bResult = (bTrue || bUnknown); //error occurs here

alert(bResult); //this line never executes

Multiplicative operators

This next section deals with the three multiplicative operators: multiple, divide, and modulus. These

operators work in a manner similar to their counterparts in languages such as Java, C, and Perl, but they

also include some automatic type conversions you need to be aware of.

Multiply

The multiply operator is represented by an asterisk (*) and is used, as one might suspect, to multiply two

numbers. The syntax is the same as in C:

var iResult = 34 * 56;

However, the multiply operator also has some unique behaviors when dealing with special values:

?

If the operands are numbers, regular arithmetic multiply is performed, meaning that two posi-

tives or two negatives equal a positive, whereas operands with different signs yield a negative.

If the result is too high or too low, the result is either

Infinity

or –

Infinity

.

?

If either operand is

NaN

, the result is

NaN

.

?

If

Infinity

is multiplied by 0, the result is

NaN

.

?

If

Infinity

is multiplied by any number other than 0, the result is either

Infinity

or

–

Infinity

, depending on the sign of the second operand.

?

If

Infinity

is multiplied by

Infinity

, the result is

Infinity

.

46

Chapter 2

05_579088 ch02.qxd 3/28/05 11:35 AM Page 46

Free JavaScript Editor
Ajax Editor

©

↓