| ★ wanayoo — archive 1999 http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Arithmetic_Operators | Nouvelle recherche | Portail wanayoo |
Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/).
These operators work as they do in most other programming languages when used with floating point numbers (in particular, note that division by zero produces NaN). For example:
1 / 2 // returns 0.5 in JavaScript 1 / 2 // returns 0 in Java (neither number is explicitly a floating point number) 1.0 / 2.0 // returns 0.5 in both JavaScript and Java
| Operators | |
| Implemented in: | JavaScript 1.0 |
| ECMA Version: | ECMA-262 |
The modulus operator is used as follows:
var1 % var2
The modulus operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the preceding statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2. For example, 12 % 5 returns 2. The result will have the same sign as var1; that is, −1 % 2 returns −1.
The increment operator is used as follows:
var++ or ++var
This operator increments (adds one to) its operand and returns a value. If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing. If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing.
For example, if x is three, then the statement y = x++ sets y to 3 and increments x to 4. If x is 3, then the statement y = ++x increments x to 4 and sets y to 4.
The decrement operator is used as follows:
var-- or --var
This operator decrements (subtracts one from) its operand and returns a value. If used postfix (for example, x--), then it returns the value before decrementing. If used prefix (for example, --x), then it returns the value after decrementing.
For example, if x is three, then the statement y = x-- sets y to 3 and decrements x to 2. If x is 3, then the statement y = --x decrements x to 2 and sets y to 2.
Page last modified 14:09, 2 May 2008 by Tgr?