# Prefix and postfix type in JavaScript

In fact, the increment and decrement operations can be written in two ways. Let's look at these methods using the example `++`, and for `--` everything will be similar.

In the first method, the operation is written after the variable name, like this: `a++`, and in the second method, before the variable name, like this: `++a`.

The first form is called postfix, and the second is called prefix. Let's see in what cases the difference between these two forms manifests itself.

Suppose we have the code `alert(a++)` and the code `alert(++a)`.

In the case of `alert(a++)` the variable will first be displayed and then incremented by one, while in the case of `alert(++a)` the variable will first be incremented by one and then displayed.

In the following example, the first `alert` will output `0`, as the screen output will work first and then the variable will be incremented:

`let num = 0; alert(num++); // shows 0 alert(num); // shows 1`

And now the variable will first increase, and only then there will be an output to the screen:

`let num = 0; alert(++num); // shows 1 - variable increased immediately`

This behavior acts not only for displaying, but also for assigning:

`let num1 = 0; let num2 = num1++; // 0 will be written to the variable num2 alert(num2); // shows 0 alert(num1); // shows 1 - variable num1 changed after being written to num2`

Now let's change the postfix form to the prefix form:

`let num1 = 0; let num2 = ++num1; // 1 will be written to the variable num2 alert(num2); // shows 1`

If our operation is performed on a separate line, then there is no difference between the prefix and postfix forms:

`let num = 0; ++num; num++; alert(num); // shows 2`

Without running the code, determine what will be displayed on the screen:

`let num = 3; alert(++num);`

Without running the code, determine what will be displayed on the screen:

`let num = 3; alert(num++);`

Without running the code, determine what will be displayed on the screen:

`let num = 3; alert(--num);`

Without running the code, determine what will be displayed on the screen:

`let num = 3; alert(num--);`

Without running the code, determine what will be displayed on the screen:

`let num1 = 3; let num2 = ++num1; alert(num1); alert(num2);`

Without running the code, determine what will be displayed on the screen:

`let num1 = 3; let num2 = num1++; alert(num1); alert(num2);`

Without running the code, determine what will be displayed on the screen:

`let num1 = 3; let num2 = --num1; alert(num1); alert(num2);`

Without running the code, determine what will be displayed on the screen:

`let num1 = 3; let num2 = num1--; alert(num1); alert(num2);`

Without running the code, determine what will be displayed on the screen:

`let num1 = 3; num1++; let num2 = num1--; alert(num1++); alert(--num2);`
← →
↑
en ru