TypeScript Operators
Learn how to use arithmetic, comparison, logical, assignment, and ternary operators in TypeScript with practical examples and expected outputs.
What Will We Learn?
Operators are important when writing conditions, calculating fees, validating data, and implementing application logic.
- Perform mathematical calculations using arithmetic operators.
- Compare values using comparison operators.
- Combine conditions using logical operators.
- Assign and update values using assignment operators.
- Select a value using the ternary operator.
TypeScript Operators Workflow
Follow these steps to understand how operators process values in a TypeScript program.
Step 1
Input ValuesDeclare values or variables.
Step 2
Apply OperatorPerform the required operation.
Step 3
Get ResultCalculate or evaluate the result.
Step 4
Display ResultPrint the result using console.log().
1. Arithmetic Operators
Arithmetic operators perform mathematical operations such as addition, subtraction, multiplication, and division.
Example
let a: number = 20;
let b: number = 10;
console.log("Addition: " + (a + b));
console.log("Subtraction: " + (a - b));
console.log("Multiplication: " + (a * b));
console.log("Division: " + (a / b));
console.log("Remainder: " + (a % b));
Expected Output
Addition: 30
Subtraction: 10
Multiplication: 200
Division: 2
Remainder: 0
Common Arithmetic Operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 10 + 5 |
15 |
- |
Subtraction | 10 - 5 |
5 |
* |
Multiplication | 10 * 5 |
50 |
/ |
Division | 10 / 5 |
2 |
% |
Remainder | 10 % 3 |
1 |
** |
Exponentiation | 2 ** 3 |
8 |
2. Comparison Operators
Comparison operators compare two values and return
a boolean result: true or false.
Example
let a: number = 20;
let b: number = 10;
console.log(a > b);
console.log(a < b);
console.log(a === b);
console.log(a !== b);
console.log(a >= 20);
console.log(b <= 10);
Expected Output
true
false
false
true
true
true
Comparison operators are useful in conditions such as checking marks, age limits, fees, and eligibility.
3. Logical Operators
Logical operators combine conditions or reverse a
boolean result. They are commonly used with
if statements.
Example
let hasPaidFees: boolean = true;
let hasCompletedForm: boolean = true;
let isBlocked: boolean = false;
console.log(hasPaidFees && hasCompletedForm);
console.log(hasPaidFees || isBlocked);
console.log(!hasPaidFees);
console.log(!isBlocked);
Expected Output
true
true
false
true
Logical Operators
| Operator | Name | Meaning |
|---|---|---|
&& |
Logical AND | Both conditions must be true. |
|| |
Logical OR | At least one condition must be true. |
! |
Logical NOT | Reverses a boolean value. |
4. Assignment Operators
Assignment operators assign a value to a variable or update its current value.
Example
let fees: number = 1000;
fees += 500;
console.log(fees);
fees -= 200;
console.log(fees);
fees *= 2;
console.log(fees);
fees /= 2;
console.log(fees);
Expected Output
1500
1300
2600
1300
Common Assignment Operators
| Operator | Equivalent Expression |
|---|---|
= |
a = 10 |
+= |
a = a + value |
-= |
a = a - value |
*= |
a = a * value |
/= |
a = a / value |
%= |
a = a % value |
5. Ternary Operator
The ternary operator is a short way to choose one of
two values based on a condition. It uses the
? and : symbols.
Syntax
condition ? valueIfTrue : valueIfFalse;
Example
let marks: number = 75;
let result = marks >= 35 ? "Pass" : "Fail";
console.log(result);
Expected Output
Pass
Because marks is at least 35, the condition
is true and the expression returns "Pass".
6. Operator Precedence
Operator precedence determines which operation is evaluated first when an expression contains multiple operators. Parentheses can make the intended order explicit.
Example
let result1 = 10 + 5 * 2;
let result2 = (10 + 5) * 2;
console.log(result1);
console.log(result2);
Expected Output
20
30
Multiplication has higher precedence than addition. Parentheses make addition happen first in the second expression.
Practical Example: CIIT Course Fees
Let's combine arithmetic, comparison, logical, assignment, and ternary operators in a course fee calculation.
Example
let courseFees: number = 35000;
let discount: number = 5000;
let finalFees = courseFees - discount;
let isEligible: boolean = true;
let hasPaidFees: boolean = false;
let admissionStatus =
isEligible && !hasPaidFees ? "Payment Pending" : "Check Status";
console.log("Course Fees: " + courseFees);
console.log("Discount: " + discount);
console.log("Final Fees: " + finalFees);
console.log("Admission Status: " + admissionStatus);
finalFees += 1000;
console.log("Updated Fees: " + finalFees);
Expected Output
Course Fees: 35000
Discount: 5000
Final Fees: 30000
Admission Status: Payment Pending
Updated Fees: 31000
This example uses arithmetic for fees, logical operators for status evaluation, a ternary operator for selecting a message, and an assignment operator to update the total.
Common Mistakes
-
Using
=when comparison with===is intended. - Forgetting parentheses when an expression needs a particular order of evaluation.
-
Confusing logical AND (
&&) with logical OR (||). - Updating a variable without checking its current value.
- Writing a nested ternary expression that is difficult to read instead of using an if/else statement.
=== when
comparing values, and use parentheses to make complex
expressions easier to understand.
Practice Task
Create a TypeScript program for a student course admission system using operators.
- Course fees: 40000
- Discount: 6000
- Marks: 70
- Minimum passing marks: 35
- Registration completed: true
Calculate the final fees, compare the marks with the passing marks, and display the registration status using logical and ternary operators.
Hint
let courseFees = 40000;
let discount = 6000;
let marks = 70;
let passingMarks = 35;
let registrationCompleted = true;
let finalFees = courseFees - discount;
let result = marks >= passingMarks ? "Pass" : "Fail";
console.log(finalFees);
console.log(result);
console.log(registrationCompleted);
Expected Output
34000
Pass
true
Summary
In this lesson, we introduced arithmetic, comparison, logical, assignment, and ternary operators. We also explored operator precedence and applied operators in a practical course fee calculation.
Key Point: Operators allow a TypeScript program to calculate values, compare conditions, update variables, and choose results.