1

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?

Urooj
  • 151
  • 7
  • Just check the [doc](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment) – Alexis Feb 20 '23 at 10:21
  • 2
    It's a post increment, it will be incremented after being evaluated. – Matthieu Riegler Feb 20 '23 at 10:21
  • 3
    Does this answer your question? [++someVariable vs. someVariable++ in JavaScript](https://stackoverflow.com/questions/3469885/somevariable-vs-somevariable-in-javascript) – Matthieu Riegler Feb 20 '23 at 10:21
  • 2
    And what's wrong? Function is called with old value ` 0`, then value is decremented. Javascript basics. Nothing to do with angular and typescript. – Edmunds Folkmanis Feb 20 '23 at 10:23

1 Answers1

1

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);

yarally
  • 26
  • 2