This content originally appeared on DEV Community and was authored by
What is the result of this code?
let count = 0;
console.log(count++)
....it's...
0
Let's learn about post-increment and pre-increment
The pre-increment and pre-decrement operators increment (or decrement) their operand by 1, and the value of the expression is the resulting incremented (or decremented) value.
The post-increment and post-decrement operators increase (or decrease) the value of their operand by 1, but the value of the expression is the operand's value before the increment (or decrement) operation. - Wikipedia
Let's look at what's going on "under the hood".
Here is a representation using a JavaScript class object.
postAdd = {count++}
preAdd = {++count}
class Counter {
constructor (){
this.count = 0
}
postAdd(){
let oldCount = this.count;
this.count+=1;
return oldCount
}
preAdd(){
this.count += 1;
return this.count
}
}
let count = new Counter();
console.log(count.postAdd()) // 0
console.log(count.preAdd()) // 2 (the first console.log still increments!!!)
In my own words
{variable}++ can be used when you don't reference the increased variable in that function call and ++{variable} is useful when you need to reference the incremented variable immediately. -Zach Stone
Technically, pre incrementent is also faster since it is not creating a new variable each time.
How do you decide whether to use pre or post increment?
I would love to hear your thoughts in the comments section below.
Feel free to contact me using my website.
Have a fantastic day!
This content originally appeared on DEV Community and was authored by
| Sciencx (2022-02-04T00:47:19+00:00) count++ !== ++count. Retrieved from https://www.scien.cx/2022/02/04/count-count/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.