TypeScript Assignment Operators with Examples

Assignment operators are the backbone of programming languages.

They allow us to assign values to variables, perform calculations, and manage data efficiently.

Without them, even the simplest programs would become complex and unwieldy.

So, without further a do let’s learn the assignment operators and explore their various types and uses.

What is Assignment Operators in TypeScript?

Assignment operators in TypeScript are used to assign values to variables.

They combine assignment with another operation, such as addition, subtraction, or bitwise operations, making the code more concise and readable.

List of TypeScript Assignment operators

1. Simple or the Basic Assignment (=) operator

The simple assignment (=) operator designates values from the operand on the right and assign to the operand on the left.

Let’s check out the illustration:

Z = X + Y  will assign the value of X + Y into Z

Let’s try to put it in an example:

let SampleNumber1: number = 5;
let SampleNumber2 = 10;

So, as you can see SampleNumber1= SampleNumber2; // Now, SampleNumber1 has the value 10

2. Add and Assignment (+=) operator

The (+=) operator sums the right operand and the left operand, then assigns the resulting value to the left operand.

Here’s the illustration:

y += x; // Equivalent to y = y + x

Here’s an example:

let x: number = 15,
y: number = 20
y += x // y = y + x ✅
console.log(y) 

Output:

35

3. Subtract and Assignment (-=) operator

The (-=) operator deducts the right operand from the left operand and then assigns the outcome to the left operand.

Here’s the illustration:

y -= x; // Equivalent to y = y +-x

Here’s an example:

let x: number = 15,
y: number = 20
y -= x // y = y - x ✅
console.log(y) 

Output:

5

4. Multiply and Assignment (*=) operator

The (*=) operator multiplies the right operand with the left operand and then assigns the resulting value to the left operand.

Here’s the illustration:

Y *= X is equivalent to Y = Y * X

Here’s an example:

let x: number = 15,
y: number = 20
y *= x // y = y * x ✅
console.log(y) 

Output:

300

5. Divide and Assignment (/=) operator

The (/=) operator divides the left operand by the right operand and then assigns the resulting value to the left operand.

Here’s the illustration:

y /= x  is equivalent to = y / x

Here’s an example:

let x: number = 15,
y: number = 20
y /= x // y = y / x ✅
console.log(y) 

Output:

1.3333333333333333

Conclusion

Finally, we’re done learning the assignment operators in TypeScript, which is used to assign values to variables.

The simple assignment operator (=) is the basic operator that designates values from the operand on the right and assign to the operand on the left.

Other assignment operators, like +=, -=, *=, and /=, execute an operation and assignment into a single step.

I hope this article has given you some insights and helped you understand the TypeScript Assignment Operators.

If you have any questions or inquiries, please don’t hesitate to leave a comment below.

Leave a Comment