Search code examples
angulartypescriptoperators

Increment/decrement operator with this keyword in angular


I have a component with a property count=0;. When inside the ts code i write

console.log(this.count--);

it prints 0. What is wrong?


Solution

  • What you are trying to execute has nothing to do with Angular in particular, it is just plain javascript. By adding the -- operator to the end of the expression, you use the value first, and then update it. console.log(this.count--); is therefore equivalent to: console.log(this.count); this.count = this.count - 1; I believe that you can achieve your expected result by moving the operator to the beginning: console.log(--this.count); which would evaluate to: this.count = this.count - 1; console.log(this.count);