JavaScript Operators
Arithmetic Operators (given y = 5
)
Operator |
Description |
Example |
Result |
+ |
Addition |
x = y + 2 |
x = 7 |
y = 5 |
- |
Subtraction |
x = y - 2 |
x = 3 |
y = 5 |
* |
Multiplication |
x = y * 2 |
x = 10 |
y = 5 |
/ |
Division |
x = y / 2 |
x = 2.5 |
y = 5 |
% |
Modulus (division remainder) |
x = y % 2 |
x = 1 |
y = 5 |
++ |
Increment |
x = ++y |
x = 6 |
y = 6 |
x = y++ |
x = 5 |
y = 6 |
-- |
Decrement |
x = --y |
x = 4 |
y = 4 |
x = y-- |
x = 5 |
y = 4 |
Assignment Operators (given x = 10
and y = 5
)
Operator |
Example |
Same as |
Result |
= |
x = y |
|
x = 5 |
+= |
x += y |
x = x + y |
x = 15 |
-= |
x -= y |
x = x - y |
x = 5 |
*= |
x *= y |
x = x * y |
x = 50 |
/= |
x /= y |
x = x / y |
x = 2 |
%= |
x %= y |
x = x % y |
x = 0 |
String Operator
The + operator can also be used to add string variables or text values together.
If you add a number and a string, the result will be a string.
For example,
5 + 5 = 10
"5" + "5" = "55"
"5" + 5 = "55"
|
|
|
Demonstration
The following demonstration shows how the script of HTML and JavaScript is displayed on the Web.