Operators
Operators are the symbols used to work with variables. You’re already familiar with operators from simple arithmetic; plus and minus are operators. See Table 1.3 for the full list of operators.
Operator | What it does |
---|---|
x + y (Numeric) | Adds x and y together |
x + y (String) | Concatenates x and y together |
x - y | Subtracts y from x |
x * y | Multiplies x and y together |
x / y | Divides x by y |
x % y | Modulus of x and y (i.e., the remainder when x is divided by y) |
x++, ++x | Adds one to x (same as x = x + 1) |
x--, --x | Subtracts one from x (same as x = x - 1) |
-x | Reverses the sign on x |
✓ Tips
While both x++ and ++x add one to x, they are not identical; the former increments x after the assignment is complete, and the latter before. For example, if x is 5, ...
Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.