Essential JavaScript

Going Further

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:

OperatorSymbol
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

  • I can do math operations in JavaScript without repeating the variable name

Code Editor

Click "Run Code" to execute your JavaScript in a secure sandbox and see the output below.

Console Output

// Console output will appear here...

0 output lines