An operator is a special character (or character grouping) that causes the computer to perform a specific action, such as addition. We've already seen the assignment operator (=) that assigns the value on its right-hand side to the variable on its left-hand side. Other operators include the mathematical operators: +, -, *, / and % for addition, subtraction, multiplication, division and modulus respectively.
JavaScript, like many other programming languages, doesn't treat division the same way that your grade six maths teacher did. In JavaScript, division is separated into two separate operations - one to divide, and one to get the remainder (the modulus operation). Using the division operator, the results are much as you would expect them to be. However, the modulus performs the unique task of extracting a whole number remainder. For example, 10%3 is equal to one (three goes into 10 three times with one left over). Similarly, 7%3 is equal to one.
Recalling the assignment operator (=) which assigns the value on its right-hand side to the variable on its left-hand side, the syntax for arithmetic operators is:
Syntax: variable = operand operator operand; |
or
total = x + y;