Learn

Variables

Mathematical Assignment Operators

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 `w`

from `4`

to `5`

.

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: `-=`

, `*=`

, and `/=`

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!

Use the `+=`

mathematical assignment operator to increase the value stored in `levelUp`

by `5`

.

Use the `-=`

mathematical assignment operator to decrease the value stored in `powerLevel`

by `100`

.

Use the `*=`

mathematical assignment operator to multiply the value stored in `multiplyMe`

by `11`

.

Use the `/=`

mathematical assignment operator to divide the value stored in `quarterMe`

by `4`

.