Multiplicative operators are those that require multiplication or division to evaluate their result.
The following table lists all operators are multiplicative and those, which are classified under other categories, which are also multiplicative, by implication:
Operator | Description |
---|---|
% | Remainder |
* | Multiply |
/ | Divide |
*= | Multiply and assign to an LValue |
/= | Divide and assign to an LValue |
%= | Remainder and assign to an LValue |
Operands that are used with multiplicative operators must have numeric values or be convertible using the valueOf() method.
The order of evaluation of the operands is described in the ECMA standard and a fully compliant implementation should evaluate from left to right. This means that the expression:
myFunctionA() * myFunctionB()
should evaluate myFunctionA() before myFunctionB() and any side effects of executing myFunctionA() will precede those for myFunctionB().
However, you should test this and not rely on it being portable across implementations.
Beware of expressions like this:
0 * myFunction()
Even though you may not have put the zero in as a constant, the operand on the left may evaluate to be zero. Some implementations may provide performance enhancements that depend on eliminating unnecessary computation. A zero multiplied by any other value will yield a zero. The function may never be executed.
If you really want myFunctionA(), myFunctionB() or myFunction() to be executed reliably and in a portable manner, you should evaluate them outside of the expression and assign their results to variables, which you can then use in the expression in their place.
There is a very marginal performance hit but the scripts will execute more reliably across a wider variety of platforms.
Prev | Home | Next |
Multiplicative expression | Up | Multiply (*) |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |