mozilla

# Revision 355453 of Arithmetic operators

• Revision slug: JavaScript/Reference/Operators/Arithmetic_Operators
• Revision title: Arithmetic Operators
• Revision id: 355453
• Created:
• Creator: dbruant
• Is current revision? No
• Comment Remainder and not modulo. Suggested by Axel Rauschmayer
Tags:

## Revision Content

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 `+/-Infinity`). 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

2.0 / 0    // returns Infinity in Javascript
2.0 / 0.0  // returns Infinity too
2.0 / -0.0 // returns -Infinity in Javascript

```
 Operators Implemented in: JavaScript 1.0 ECMA Version: ECMA-262

## % (Remainder)

The remainder operator is used as follows:

`var1 % var2 `

The remainder 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`.

There is a proposal to get an actual modulo operator in a future version of ECMAScript

## ++ (Increment)

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.

## -- (Decrement)

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.

## - (Unary Negation)

The unary negation operator precedes its operand and negates it. For example, `y = -x` negates the value of `x` and assigns that to `y`; that is, if `x` were 3, `y` would get the value -3 and `x` would retain the value 3.

## + (Unary Plus)

The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already. For example, `y = +x` takes the value of `x` and assigns that to `y`; that is, if `x` were 3, `y` would get the value 3 and `x` would retain the value 3; but if x were the string "3", y would also get the value 3. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. It can convert string representations of integers and floats, as well as the non-string values true, false, and null. Integers in both decimal and hexadecimal ("0x"-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate to NaN.

## Revision Source

```<p>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 (/).</p>
<p>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 <a class="internal" href="/en/JavaScript/Reference/Global_Objects/Infinity" title="en/Core JavaScript 1.5 Reference/Global Properties/Infinity"><code>+/-Infinity</code></a>). For example:</p>
<pre class="eval">
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

2.0 / 0    // returns Infinity in Javascript
2.0 / 0.0  // returns Infinity too
2.0 / -0.0 // returns -Infinity in Javascript

</pre>
<table class="standard-table">
<tbody>
<tr>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.0</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262</td>
</tr>
</tbody>
</table>
<h2 id=".25_.28Modulus.29" name=".25_.28Modulus.29">&nbsp;% (Remainder)</h2>
<p>The remainder operator is used as follows:</p>
<p><code><em>var1</em>&nbsp;% <em>var2</em> </code></p>
<p>The remainder operator returns the first operand modulo the second operand, that is, <code>var1</code> modulo <code>var2</code>, in the preceding statement, where <code>var1</code> and <code>var2</code> are variables. The modulo function is the integer remainder of dividing <code>var1</code> by <code>var2</code>. For example, <code>12&nbsp;% 5</code> returns <code>2</code>. The result will have the same sign as <em>var1</em>; that is, <code>-1&nbsp;% 2</code> returns <code>-1</code>.</p>
<p><a href="http://wiki.ecmascript.org/doku.php?id=strawman:modulo_operator" title="http://wiki.ecmascript.org/doku.php?id=strawman:modulo_operator">There is a proposal to get an actual modulo operator in a future version of ECMAScript</a></p>
<h2 id=".2B.2B_.28Increment.29" name=".2B.2B_.28Increment.29">++ (Increment)</h2>
<p>The increment operator is used as follows:</p>
<p><code><em>var</em>++</code> or <code>++<em>var</em></code></p>
<p>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.</p>
<p>For example, if <code>x</code> is three, then the statement <code>y = x++</code> sets <code>y</code> to 3 and increments <code>x</code> to 4. If <code>x</code> is 3, then the statement <code>y = ++x</code> increments <code>x</code> to 4 and sets <code>y</code> to 4.</p>
<h2 id="--_.28Decrement.29" name="--_.28Decrement.29">-- (Decrement)</h2>
<p>The decrement operator is used as follows:</p>
<p><code><em>var</em>--</code> or <code>--<em>var</em></code></p>
<p>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.</p>
<p>For example, if <code>x</code> is three, then the statement <code>y = x--</code> sets <code>y</code> to 3 and decrements <code>x</code> to 2. If <code>x</code> is 3, then the statement <code>y = --x</code> decrements <code>x</code> to 2 and sets <code>y</code> to 2.</p>
<h2 id="-_.28Unary_Negation.29" name="-_.28Unary_Negation.29">- (Unary Negation)</h2>
<p>The unary negation operator precedes its operand and negates it. For example, <code>y = -x</code> negates the value of <code>x</code> and assigns that to <code>y</code>; that is, if <code>x</code> were 3, <code>y</code> would get the value -3 and <code>x</code> would retain the value 3.</p>
<h2 id="-_.28Unary_Negation.29" name="-_.28Unary_Negation.29">+ (Unary Plus)</h2>
<p>The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already. For example, <code>y = +x</code> takes the value of <code>x</code> and assigns that to <code>y</code>; that is, if <code>x</code> were 3, <code>y</code> would get the value 3 and <code>x</code> would retain the value 3; but if x were the string "3", y would also get the value 3. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. It can convert string representations of integers and floats, as well as the non-string values true, false, and null. Integers in both decimal and hexadecimal ("0x"-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate to NaN.</p>```