# Revision 27604 of Arithmetic operators

• Revision slug: JavaScript/Reference/Operators/Arithmetic_Operators
• Revision title: Arithmetic Operators
• Revision id: 27604
• Created:
• Creator: Tgr
• Is current revision? No
• Comment /* % (Modulus) */ negative numbers
Tags:

## Revision Content

### Summary

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

### % (Modulus)

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

### ++ (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.

{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Operadores/Aritm\u00e9ticos", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Op\u00e9rateurs/Op\u00e9rateurs_arithm\u00e9tiques", "ja": "ja/Core_JavaScript_1.5_Reference/Operators/Arithmetic_Operators", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Operatory/Operatory_arytmetyczne" } ) }}

## Revision Source

```<p>
</p>
<h3 name="Summary"> Summary </h3>
<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 href="en/Core_JavaScript_1.5_Reference/Global_Properties/NaN"><code>NaN</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
</pre>
<table class="fullwidth-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>
<h3 name=".25_.28Modulus.29"> % (Modulus) </h3>
<p>The modulus operator is used as follows:
</p><p><code>
<i>var1</i> % <i>var2</i>
</code>
</p><p>The modulus 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 % 5</code> returns <code>2</code>. The result will have the same sign as <i>var1</i>; that is, <code>−1 % 2</code> returns <code>−1</code>.
</p>
<h3 name=".2B.2B_.28Increment.29"> ++ (Increment) </h3>
<p>The increment operator is used as follows:
</p><p><code><i>var</i>++</code> or <code>++<i>var</i></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>
<h3 name="--_.28Decrement.29"> -- (Decrement) </h3>
<p>The decrement operator is used as follows:
</p><p><code><i>var</i>--</code> or <code>--<i>var</i></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>
<h3 name="-_.28Unary_Negation.29"> - (Unary Negation) </h3>
<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><p><br>
</p><p><br>
</p>
<div class="noinclude">
</div>
{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Operadores/Aritm\u00e9ticos", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Op\u00e9rateurs/Op\u00e9rateurs_arithm\u00e9tiques", "ja": "ja/Core_JavaScript_1.5_Reference/Operators/Arithmetic_Operators", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Operatory/Operatory_arytmetyczne" } ) }}```