Increment & Decrement Operators in Typescript

We use the increment & Decrement operators to increase or decrease the value of the variable by one. Typescript uses the ++ (increment) & -- (decrement) to denote them. We can either prefix or Postfix these operators.

Syntax of Increment & Decrement Operator

Increment Operator ++x or x++. This is equal to x=x+1

Decrement Operator --x or x--. This is equal to x=x-1

Example

Prefix & Postfix

There are two ways you can use the operator. One is before the operand, which is known as Prefix. The other method is to use it after the operand, which is known as Postfix.

Prefix Example

Postfix Example

Difference Between Prefix & Postfix

When we use the ++ operator as a prefix as in ++a

  1. The value of the variable a is incremented by 1
  2. Then it returns the value.

When we use the ++ operator as a Postfix as in a++,

  1. The value of the variable is returned.
  2. Then the variable a is incremented by 1

Reference

  1. Expressions & Operators
  2. Precedence & Associativity

Read More

  1. Complete Typescript Tutorial
  2. Typescript Operators
  3. Arithmetic Operators
  4. Unary plus / Unary minus Operators
  5. Increment/Decrement Operators
  6. Comparison / Relational Operators
  7. Equality Operator / Strict Equality Operators
  8. Ternary Conditional Operators
  9. Logical Operators
  10. Bitwise Operators
  11. Assignment Operators
  12. Nullish coalescing operator
  13. Comma Operator in Typescript
  14. Operator Precedence

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top