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.

Table 1.3. Operators
OperatorWhat it does
x + y (Numeric)Adds x and y together
x + y (String)Concatenates x and y together
x - ySubtracts y from x
x * yMultiplies x and y together
x / yDivides x by y
x % yModulus of x and y (i.e., the remainder when x is divided by y)
x++, ++xAdds one to x (same as x = x + 1)
x--, --xSubtracts one from x (same as x = x - 1)
-xReverses 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.