Assignment Operators in TypeScript

TypeScript assignment operators are used to assign values to variables and expressions. They are the fundamental building blocks for manipulating data and updating the state of your program.

List of Assignment Operators

Here are the different assignment operators available in TypeScript:

  1. Simple assignment (=): Assigns the right operand value to the left operand.
  2. Addition assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
  3. Subtraction assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
  4. Multiplication assignment (*=): Multiplies the right operand with the left operand and assigns the result to the left operand.
  5. Division assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
  6. Modulo assignment (%=): Finds the remainder of dividing the left operand by the right operand and assigns the result to the left operand.
  7. Exponential assignment (**=): Raises the left operand to the power of the right operand and assigns the result to the left operand.
  8. Left shift assignment (<<=): Shifts the left operand bitwise to the left by the number of bits specified by the right operand and assigns the result to the left operand.
  9. Right shift assignment (>>=): Shifts the left operand bitwise to the right by the number of bits specified by the right operand and assigns the result to the left operand.
  10. Unsigned right shift assignment (>>>=): Shifts the left operand bitwise to the right by the number of bits specified by the right operand, treating the left operand as an unsigned integer, and assigns the result to the left operand.

Assignment (=) Operator

The basic assignment operator (=) is used to assign a value to a variable.

let x: number; x = 5; // Assign the value 5 to variable x

Compound Assignment Operators

Compound assignment operators combine an assignment with another operation, making it more concise. They include:

Addition Assignment (+=)

let y: number = 10; y += 3; // Equivalent to y = y + 3; console.log(y); // Result: 13

Subtraction Assignment (-=)

let z: number = 8; z -= 2; // Equivalent to z = z - 2; console.log(z); // Result: 6

Multiplication Assignment (*=)

let a: number = 4; a *= 5; // Equivalent to a = a * 5; console.log(a); // Result: 20

Division Assignment (/=)

let b: number = 15; b /= 3; // Equivalent to b = b / 3; console.log(b); // Result: 5

Modulus Assignment (%=)

let c: number = 17; c %= 5; // Equivalent to c = c % 5; console.log(c); // Result: 2

These compound assignment operators perform the specified operation and then assign the result back to the variable.

Exponentiation Assignment (**=)

The exponentiation assignment operator (**=) raises the left operand to the power of the right operand and assigns the result to the left operand.

let base: number = 2; base **= 3; // Equivalent to base = base ** 3; console.log(base); // Result: 8

Type Safety

TypeScript assignment operators ensure type safety by restricting assignments to compatible data types. This prevents errors and unexpected behavior due to mismatched types.

Operator Precedence

Assignment operators have a specific order of precedence within expressions. Understanding this order is crucial for accurate evaluation and avoiding ambiguity in your code.

Chaining Assignments

You can chain multiple assignment operators together to perform multiple operations with concise syntax.

Conclusion

TypeScript assignment operators, including the basic assignment (=) and compound assignments (+=, -=, *=, /=, %=), provide a concise way to assign values and perform compound operations on variables. These operators enhance code readability and efficiency by combining assignment with arithmetic or modulus operations in a single step.