Programming Language
TypeScript
Operators
Assignment Operators

TypeScript Assignment Operators

Assignment operators are used to assign values to variables in TypeScript. They can also combine assignment with mathematical or logical operations, making the code shorter and easier to read.

List of Assignment Operators

= (Simple Assignment)

Assigns the value on the right to the variable on the left.

let a = 5;
console.log(a); // Output: 5

+= (Add and Assign)

Adds the value on the right to the variable on the left, then assigns the result to the variable.

let a = 5;
a += 3; // Same as a = a + 3
console.log(a); // Output: 8

-= (Subtract and Assign)

Subtracts the value on the right from the variable on the left, then assigns the result to the variable.

let a = 5;
a -= 2; // Same as a = a - 2
console.log(a); // Output: 3

*= (Multiply and Assign)

Multiplies the variable by the value on the right, then assigns the result to the variable.

let a = 5;
a *= 4; // Same as a = a * 4
console.log(a); // Output: 20

/= (Divide and Assign)

Divides the variable by the value on the right, then assigns the result to the variable.

let a = 10;
a /= 2; // Same as a = a / 2
console.log(a); // Output: 5

%= (Modulus and Assign)

Finds the remainder when the variable is divided by the value on the right, then assigns the result to the variable.

let a = 10;
a %= 3; // Same as a = a % 3
console.log(a); // Output: 1

**= (Exponentiation and Assign)

Raises the variable to the power of the value on the right, then assigns the result to the variable.

let a = 2;
a **= 3; // Same as a = a ** 3 (2 to the power of 3)
console.log(a); // Output: 8

Simple Assignment Operator

OperatorDescriptionExampleResult
=Assigns the value on the right to the variable on the left.let a = 5a = 5

Compound Assignment Operators

These operators perform a calculation and then assign the result to the variable.

OperatorDescriptionExampleResult
+=Adds the value on the right to the variable on the left, then assigns the result.let a = 5; a += 3a = 8
-=Subtracts the value on the right from the variable on the left, then assigns the result.let a = 5; a -= 3a = 2
*=Multiplies the variable by the value on the right, then assigns the result.let a = 5; a *= 3a = 15
/=Divides the variable by the value on the right, then assigns the result.let a = 6; a /= 3a = 2
%=Finds the remainder when the variable is divided by the value on the right, then assigns the result.let a = 5; a %= 2a = 1

Bitwise Assignment Operators

These operators perform bitwise calculations and assign the result to the variable.

OperatorDescriptionExampleResult
&=Performs a bitwise AND operation and assigns the result.let a = 5; a &= 3a = 1
│=Performs a bitwise OR operation and assigns the result.let a = 5; a │= 3a = 7
^=Performs a bitwise XOR operation and assigns the result.let a = 5; a ^= 3a = 6
<<=Shifts the bits of the variable to the left and assigns the result.let a = 5; a <<= 1a = 10
>>=Shifts the bits of the variable to the right and assigns the result.let a = 5; a >>= 1a = 2
>>>=Shifts the bits of the variable to the right (unsigned) and assigns the result.let a = 5; a >>>= 1a = 2

Logical Assignment Operators

These operators combine logical operations with assignments (introduced in modern JavaScript and TypeScript).

OperatorDescriptionExampleResult
&&=Assigns the value only if the variable is truthy.let a = true; a &&= falsea = false
││=Assigns the value only if the variable is falsy.let a = false; a ││= truea = true
??=Assigns the value only if the variable is null or undefined.let a = null; a ??= 5a = 5

Summary

Assignment operators make it easy to perform calculations and assign values in a single step. They are useful for writing concise and efficient code. By understanding these operators, you can simplify your programming tasks and make your TypeScript code more readable.