Assignment Operators in TypeScript

An assignment operator requires two operands. The value of the right operand is assigned to the left operand. The sign = denotes the simple assignment operator. The typescript also has several compound assignment operators, which is actually shorthand for other operators. List of all such operators are listed below

List of Assignment operators

NameSyntaxMeaning
Assignmentx = yx = y
Addition assignmentx += yx =x+ y
Subtraction assignmentx -= yx = x - y
Multiplication assignmentx *= yx = x * y
Division assignmentx /= yx = x / y
Remainder assignmentx %= yx = x % y
Exponentiation assignmentx **= yx = x ** y
Left shift assignmentx <<= yx = x << y
Right shift assignmentx >>= yx = x >> y
Unsigned right shift assignmentx >>>= yx = x >>> y
Bitwise AND assignmentx &= yx = x & y
Bitwise XOR assignmentx ^= yx = x ^ y
Bitwise OR assignmentx |= yx = x | y
Logical AND assignmentx &&= yx && (x = y)
Logical OR assignmentx ||= yx || (x = y)
Logical nullish assignmentx ??= yx ?? (x = y)

Simple Assignement Operator

The = assignment operator assigns the value of the right hand operand to the left hand variable.

Compound Assignment Operators

All operators except = listed in the table above are compound assignment operators. They are actually short hand notations

For Examples

The Addition assignment += operator adds a value to a variable.

is actually short hand for

Reference

  1. Expressions & Operators
  2. Precedency & 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. Required fields are marked *

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

Scroll to Top