Let’s consider how we can use variables and math operators to calculate new values and assign them to a variable. Check out the example below:
let w = 4; w = w + 1; console.log(w); // Output: 5
In the example above, we created the variable
w with the number
4 assigned to it. The following line,
w = w + 1, increases the value of
Another way we could have reassigned
w after performing some mathematical operation on it is to use built-in mathematical assignment operators. We could re-write the code above to be:
let w = 4; w += 1; console.log(w); // Output: 5
In the second example, we used the
+= assignment operator to reassign
w. We’re performing the mathematical operation of the first operator
+ using the number to the right, then reassigning
w to the computed value.
We also have access to other mathematical assignment operators:
/= which work in a similar fashion.
let x = 20; x -= 5; // Can be written as x = x - 5 console.log(x); // Output: 15 let y = 50; y *= 2; // Can be written as y = y * 2 console.log(y); // Output: 100 let z = 8; z /= 2; // Can be written as z = z / 2 console.log(z); // Output: 4
Let’s practice using these mathematical assignment operators!
+= mathematical assignment operator to increase the value stored in
-= mathematical assignment operator to decrease the value stored in
*= mathematical assignment operator to multiply the value stored in
/= mathematical assignment operator to divide the value stored in