Essential JavaScript

Going Further

Increment / Decrement

Earlier, we covered shorthand math operators.

This is what shorthand addition and subtraction look like:

let count = 0; count += 1; console.log(count); let retries = 3; retries -= 1; console.log(retries);

For the addition example, notice how we are only adding 1 to the value of the variable.

Adding and subtracting 1 is a very common operation in programming. To make it easier, we have special shorthand operators to add and subtract 1.

Increment and Decrement Operators

To add 1 to a variable, we use the increment operator, which is a double plus sign: ++

let count = 0; count++; console.log(count);

To subtract 1 from a variable, we use the decrement operator, which is a double minus sign: --

let retries = 3; retries--; console.log(retries);

Practice

There are several examples in the editor that use the shorthand addition operator to add just 1 to a variable.

Replace them with the increment operator to make the code cleaner.

Learning Goals

  • I can use the increment operator to add 1 to a variable

  • I can use the decrement operator to subtract 1 from a variable

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