TypeScript Assignment Operators
Learn how to assign values and update variables using simple assignment, addition assignment, subtraction assignment, multiplication assignment, division assignment, remainder assignment, and exponentiation assignment operators.
What Will We Learn?
Assignment operators make it easier to assign values and update variables in TypeScript programs.
-
Assign values using
=. -
Add values using
+=. -
Subtract values using
-=. -
Multiply values using
*=. -
Divide values using
/=. -
Calculate remainders using
%=. -
Update values using
**=.
Assignment Operators Workflow
Follow these steps to understand how an assignment operator updates a variable.
Step 1
Declare VariableCreate a variable with an initial value.
Step 2
Apply OperatorAssign or update the value.
Step 3
Update ValueStore the resulting value.
Step 4
Display ResultPrint the updated value.
1. Simple Assignment Operator (=)
The assignment operator = assigns a
value to a variable. The value on the right side
is assigned to the variable on the left side.
Syntax
variable = value;
Example
let studentName: string;
studentName = "Rahul";
console.log(studentName);
Expected Output
Rahul
First, the variable is declared. Then the value
"Rahul" is assigned to it.
2. Addition Assignment Operator (+=)
The += operator adds a value to the
current value of a variable and assigns the result
back to that variable.
Equivalent Expression
a += 5;
// Equivalent to:
a = a + 5;
Example
let totalMarks: number = 50;
totalMarks += 20;
console.log("Updated Marks: " + totalMarks);
Expected Output
Updated Marks: 70
The variable initially contains 50.
Adding 20 updates the value to
70.
3. Subtraction Assignment Operator (-=)
The -= operator subtracts a value from
a variable and stores the result back in the variable.
Equivalent Expression
a -= 10;
// Equivalent to:
a = a - 10;
Example
let courseFees: number = 35000;
courseFees -= 5000;
console.log("Remaining Fees: " + courseFees);
Expected Output
Remaining Fees: 30000
The original course fees are 35000.
After subtracting 5000, the updated
value becomes 30000.
4. Multiplication Assignment Operator (*=)
The *= operator multiplies the current
value by another value and assigns the result back
to the variable.
Example
let monthlyFees: number = 5000;
let months: number = 6;
monthlyFees *= months;
console.log("Total Fees: " + monthlyFees);
Expected Output
Total Fees: 30000
The variable is updated by multiplying
5000 by 6.
The result is 30000.
5. Division Assignment Operator (/=)
The /= operator divides a variable's
current value by another value and stores the result.
Example
let totalAmount: number = 30000;
let installments: number = 3;
totalAmount /= installments;
console.log("Amount Per Installment: " + totalAmount);
Expected Output
Amount Per Installment: 10000
Dividing the total amount by three updates the variable
to 10000.
6. Remainder Assignment Operator (%=)
The %= operator calculates the remainder
after division and assigns that remainder back to
the variable.
Example
let number: number = 17;
number %= 5;
console.log("Remainder: " + number);
Expected Output
Remainder: 2
The remainder of 17 / 5 is
2, so the variable's value becomes
2.
7. Exponentiation Assignment Operator (**=)
The **= operator raises a variable's
current value to the specified power and assigns
the result back to that variable.
Example
let number: number = 2;
number **= 3;
console.log("Result: " + number);
Expected Output
Result: 8
The expression calculates 2 ** 3,
which is 8, and assigns the result
back to number.
8. Combining Assignment Operators
You can use multiple assignment operators on the same variable. Each statement updates the value calculated by the previous statement.
Example
let value: number = 20;
value += 5;
console.log(value);
value -= 3;
console.log(value);
value *= 2;
console.log(value);
value /= 4;
console.log(value);
value %= 4;
console.log(value);
Expected Output
25
22
44
11
3
The starting value is 20. Each assignment
operator updates it before the next statement runs.
9. Assignment Operators Comparison
| Operator | Name | Example | Equivalent Expression |
|---|---|---|---|
= |
Assignment | a = 10 |
Assigns 10 to a |
+= |
Addition assignment | a += 5 |
a = a + 5 |
-= |
Subtraction assignment | a -= 5 |
a = a - 5 |
*= |
Multiplication assignment | a *= 5 |
a = a * 5 |
/= |
Division assignment | a /= 5 |
a = a / 5 |
%= |
Remainder assignment | a %= 5 |
a = a % 5 |
**= |
Exponentiation assignment | a **= 2 |
a = a ** 2 |
10. Assignment (=) vs Equality (===)
Assignment and comparison are different operations. Using the wrong operator can cause unexpected results or compilation errors in TypeScript.
Example
let marks: number = 80;
marks = 90;
console.log(marks === 90);
console.log(marks === 80);
Expected Output
true
false
| Operator | Purpose | Example |
|---|---|---|
= |
Assigns a value | marks = 90; |
=== |
Compares value and type | marks === 90 |
= assigns a value, while
=== checks strict equality.
11. Assignment Operators with const
Assignment operators require a target that can be
assigned. A variable declared with const
cannot be reassigned or updated with a compound
assignment operator.
Example
const courseFees: number = 35000;
// Compilation error:
// courseFees += 5000;
console.log(courseFees);
Expected Output
35000
The updating statement is commented out because
attempting to reassign courseFees
would cause a TypeScript error. The original value
remains 35000.
Practical Example: CIIT Course Fee Calculation
Let's calculate the final course fees using assignment operators in a practical example.
Example
let courseFees: number = 35000;
let discount: number = 5000;
let registrationFees: number = 1000;
courseFees -= discount;
console.log("Discounted Fees: " + courseFees);
courseFees += registrationFees;
console.log("Total Payable: " + courseFees);
courseFees /= 2;
console.log("First Installment: " + courseFees);
Expected Output
Discounted Fees: 30000
Total Payable: 31000
First Installment: 15500
The program subtracts the discount, adds the registration fees, and then divides the total into two equal installments.
Common Mistakes
-
Confusing
=with===. - Forgetting that compound assignment operators update the original variable.
-
Trying to update a variable declared with
const. - Using the wrong operator for the required calculation.
- Forgetting that division can produce a decimal result.
Practice Task
Create a TypeScript program to calculate a student's course payment using assignment operators.
- Initial fees: 40000
- Discount: 6000
- Registration fees: 1500
- Installments: 3
Subtract the discount, add the registration fees, and calculate the installment amount by dividing the updated total by three.
Hint
let fees = 40000;
fees -= 6000;
fees += 1500;
console.log("Total Fees: " + fees);
fees /= 3;
console.log("Installment: " + fees);
Expected Output
Total Fees: 35500
Installment: 11833.333333333334
Summary
In this lesson, we learned how assignment operators assign values and update variables using addition, subtraction, multiplication, division, remainder, and exponentiation.
Key Point:
Compound assignment operators provide a shorter way
to update a variable. For example,
fees += 5000 is equivalent to
fees = fees + 5000.