Cover photo: @markusspiske
++) and decrement (
But were you aware that how you use these on the operand will differ when used prefixed as opposed to postfixed?
First, let's see what happens when we use a postfixed increment operator.
Given this statement, what would you expect the console to log?
let count = 0 console.log(count++) console.log(count)
You may have been expecting it to log 1 for both, but this isn't the case. It will log 0 and then 1.
let count = 0 console.log(count++) // 0 console.log(count) // 1
Why? Because postfixed operators will return the value of the operand before applying the operator.
Now, let's try the exact same code, but with a prefixed operator:
let count = 0 console.log(++count) // 1 console.log(count) // 1
As you can see, the operator is now first applied and then the value is returned after.
count = count+1.
Either way, I hope you learned something!
Happy Coding 🤓