Essential JavaScript

Shorthand Math

We have used the addition operator to add two numbers:

1 + 2

We have also used the assignment operator to add to an existing variable:

var sum = 0;
sum = sum + 2;

JavaScript gives us a shorthand for this operation:

var sum = 0;
sum += 2;

This is called the addition assignment operator. Notice how we don't have to repeat the variable name when we use this shorthand.

Shorthand Operators List

All of the common math operations we have worked with so far have a shorthand version:

Operator Symbol
Addition Assignment +=
Subtraction Assignment -=
Multiplication Assignment *=
Division Assignment /=
Modulus Assignment %=

Using these operators can help make your code more readable.

Practice

There is code in the editor that uses the long form operators. Update the code to use the shorthand operators.


Learning Goals

Code Editor