curriculum/challenges/english/blocks/top-learn-variables-and-operators/65e1b46e500d930ce8ed90ad.md
Increasing or decreasing a number by one is among the most common numerical operations.
So, there are special operators for it:
++ increases a variable by 1:let counter = 2;
// works the same as counter = counter + 1, but is shorter
counter++;
console.log(counter); // 3
-- decreases a variable by 1:let counter = 2;
// works the same as counter = counter - 1, but is shorter
counter--;
console.log(counter); // 1
The operators ++ and -- can be placed either before or after a variable.
When the operator goes after the variable, it is in "postfix form": counter++.
The "prefix form" is when the operator goes before the variable: ++counter.
Both of these statements do the same thing: increase counter by 1.
Is there any difference? Yes, but you can only see it if you use the returned value of ++/-- .
Let’s clarify. As you know, all operators return a value. Increment/decrement is no exception. The prefix form returns the new value while the postfix form returns the old value (prior to increment/decrement).
To see the difference, here’s an example:
let counter = 1;
let a = ++counter; // (*)
console.log(a); // 2
To summarize:
let counter = 0;
counter++;
++counter;
console.log( counter ); // 2, the lines above did the same
let counter = 0;
console.log( ++counter ); // 1
let counter = 0;
console.log( counter++ ); // 0
What are the outputs of the two console.log statements in the JavaScript code below?
let counter = 1;
console.log(2 * ++counter); // Statement A
let counter = 1;
console.log(2 * counter++); // Statement B
There is no difference; both console.log statements will show the same result.
Statement A will log 4 because ++counter increments the value before the multiplication. Statement B will log 2 because counter++ increments the value after the multiplication.
Statement A will log 2 because ++counter is a syntax error. Statement B will log 4 because counter++ is the correct way to increment a counter.
Both statements will log 3 because the counter is incremented in both cases before the console.log.
2