Prefix increment 1

Prefix increment

let a = 2;
b = ++a;

Here is what the above code is Doing:
1. We declare a variable a and assign it the value of 2.
2. We declare a variable b and assign it the value of a, but before we assign it, we increment a by 1.
3. We end up with a = 3 and b = 3.

The ++ and — operators can be placed either before or after the variable.

When placed before the variable, the operator is called the prefix increment/decrement operator.
When placed after, it’s called the postfix increment/decrement operator.

The difference between the two is that the prefix version (++a) increments the variable and returns the new value,
while the postfix version (a++) returns the original value all while incrementing the variable.

Similar Posts