JavaScript Tutorial for Arithmetic Operators
- 1). Use the assignment operator -- "=" -- to assign values to variables in Javascript. For example:
var a = 3;
time = 0; - 2). Use the addition operator -- "+" -- to add values and variables together. For example:
x = length + 3;
var answer = 6 + 8; - 3). Use the subtraction operator -- "-" -- to subtract values and variables. For example:
var distance = 10 - y;
fixed = 7 - 4;
weight = pounds - 14; - 4). Use the multiplication operator -- "*" -- to multiply values and variables. For example:
waiting = 5 * 15;
var minutes = hours * 60; - 5). Use the division operator -- "/" -- to divide values and variables. For example:
total = 200 / 15;
var hours = minutes / 60; - 6). Use the modulus operator -- "%" -- to find the remainder after division. For example:
pages = count % 100;
var remainder = 78 % 6; - 7). Use the increment operator -- "++" -- to increase an integer by one. If the operator proceeds the variable, the variable is incremented and then evaluated. If it follows the variable, it is first evaluated and then incremented. For example:
var addOne = ++number;
if (++count > 10) return; (count is incremented and then compared to 10)
if (count++ > 10) return; (count is compared to 10 and then incremented) - 8). Use the decrement operator -- "--" -- to decrease an integer by one. If the operator proceeds the variable, the variable is decremented and then evaluated. If it follows the variable, it is first evaluated and then decremented. For example:
subtractOne = --number;
if (--count < 10) return; (count is decremented and then compared to 10)
if (count-- < 10) return; (count is compared to 10 and then decremented) - 9). Combine each of the arithmetic operators, excluding the increment and decrement operators, with the assignment operator to create a shorthand for simple arithmetic calculations. For example:
new += 15; (new = new + 15;)
count -= number; (count = count - number;)
var ratio *= 100; (var ratio = ratio * 100;)
fraction /= unknown; (fraction = fraction / unknown;)
modself %= factor; (modself = modself % factor;)