var usingAssignmentOperator = “the equals sign is the assignment operator”
JavaScript’s arithmetic operators are used to perform arithmetic in your code. Arithmetic operators each have a symbol known as the operator that is used to perform the operation.
let sum = 5 + 7000;
let x, y;
x = 20;
y = 30;
let z = x + y + 400;
Operator: -
Examples:
let minus = 300 - 87;
let a, b;
a = 83;
b = 65;
let c = a - b;
Operator: *
Examples:
let product = 75 * 39;
let j, k;
j = 14;
k = 92;
let l = j * k * .5;
Operator: /
Examples:
let quotient = 5016 / 6;
let q, w;
q = 46;
w = 138;
let p = (w / q) / 3; When there are multiple operations on a single line, the answer is calculated in the order of PEMDAS
Operator: %
Examples:
let modulus = 33%15; the value of modulus would be 3, because 32 goes into 15 twice, with 3 left over.
let h = 46;
let i = 13;
let g = h % i;
Operator: ++
Examples:
let someValue = 10;
someValue++;
let newValue = someValue++;
Operator: –
Examples:
let someOtherValue = 90;
someOtherValue--;
let anotherNewValue = someOtherValue--;
The operators can be used on fixed values:
let adding = 5 + 5;
let subtracting = 10 - 5;
let dividing = 40 / 5;
let multiplying = 80 * 3;
let modulos = 20 % 8;
On variables:
Arithmetic operators can also be used on variables:
let number = 50;
let newAdding = adding + number;
let newSubtracting = subtracting - number;
let newDividing = number / dividing;
let newMultiplying = multiplying * number;
let newModulos = number % modulos;
Or on a mix of both fixed values and variables:
newAdding = adding + 30;
newSubtracting = subtracting - 23;
newDividing = 5 / dividing;
newMultiplying = multiplying * 7;
newModulos = 24 % modulos;
Increment and decrement can either go before or after the variable name, for example:
myVar = 3;
myVar++
or
++myVar
Here’s the different:
let myVar = 2
document.write(myVar++)
document.write(myVar++)
document.write(myVar++)
console.log(myVar)
let myVar1 = 2
document.write(++myVar)
document.write(++myVar)
document.write(++myVar)
console.log(myVar1)
The output of the first example is 234, because the document.write is executed before the value is updated. The variable is equal to 5 after the other operations.