JavaScript: Arithmetic Special Operators (%, ++, --, - )
Arithmetic Special Operators
In addition to four standard arithmetic operators (+, -, *, /), JavaScript provides the following arithmetic operators.
JavaScript Modulus operator (%)
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 above statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2. The result will have the same sign as var1.
Example:
HTML Code
.JS Code
View the example in the browser
JavaScript Increment operator (++ )
The increment operator is used as follows:
- var1++ ( Post-increment )
- ++var1 ( Pre-increment )
In the first case (i.e. post-increment) the operator increases the variable var1 by 1 but returns the value before incrementing.
In the second case (i.e. pre-increment) the operator increases the variable var1 by 1 but returns the value after incrementing.
Example :
HTML Code
JS Code
View the example in the browser
JavaScript: Decrement operator (--)
The decrement operator is used as follows:
- var1-- ( Post-decrement )
- --var1 ( Pre-decrement )
In the first case (i.e. post-decrement) the operator decreases the variable var1 by 1 but returns the value before decrementing.
In the second case (i.e. pre-decrement) the operator decreases the variable var1 by 1 but returns the value after decrementing.
Example:
HTML Code
JS Code
View the example in the browser
JavaScript: - Unary Negation operator
-var1
The unary negation operator changes the sign of var1. When the operator negating a variable, the value of the variable remains unchanged, but the return value is negated.
Example:
HTML Code
JS Code
View the example in the browser
Practice the example online
Previous: avaScript : Arithmetic Operators
Next: JavaScript: Assignment Operators
Test your Programming skills with w3resource's quiz.