Increment and decrement operations in JavaScript

Consider the following code:

let num = 0; num = num + 1; // add the number 1 to the variable num alert(num); // shows 1

As you already know, this code can be rewritten in an shortened form through the operator +=:

let num = 0; num += 1; // add the number 1 to the variable num alert(num); // shows 1

In fact, adding one in programming is so common that an even more shortend syntax has been invented for this operation - the special operator increment ++, which increases the value of a variable by 1.

Let's rewrite our code with it:

let num = 0; num++; // add the number 1 to the variable num alert(num); // shows 1

There is also the operation decrement --, which decreases the value of a variable by 1. See an example:

let num = 0; num--; // subtract the number 1 from the variable num alert(num); // shows -1

Modify this code such a way that it contains increment and decrement operations:

let num = 10; num = num + 1; num = num + 1; num = num - 1; alert(num);

Prefix and postfix type

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);
enru