Understanding Typescript Operators
Typescript is a superset of JavaScript that adds optional static type checking, class, and interface-based object-oriented programming features. TypeScript Operators are a critical concept in TypeScript because they are used to perform arithmetic or logical operations on variables and expressions.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations on numeric data types. TypeScript provides arithmetic operators such as:
- +: Addition Operator. It is used to sum two values.
- –: Subtraction Operator. It is used to subtract one value from another.
- *: Multiplication Operator. It is used to multiply two values.
- /: Division Operator. It is used to divide the numerator by the denominator.
- %: Modulo Operator. It is used to find the remainder of the division operation.
Here’s an example of arithmetic operator usage:
“`typescript
let a = 10;
let b = 5;
console.log(a + b); // 15
console.log(a – b); // 5
console.log(a * b); // 50
console.log(a / b); // 2
console.log(a % b); // 0
“`
Comparison Operators
Comparison operators are used to compare variables or expressions. TypeScript provides comparison operators such as:
- ==: Equal to Operator. It is used to compare if two values are equal.
- !=: Not Equal to Operator. It is used to compare if two values are not equal.
- >: Greater than Operator. It is used to compare if one value is greater than another.
- <: Less than Operator. It is used to compare if one value is less than another.
- >=: Greater than or Equal to Operator. It is used to compare if one value is greater than or equal to another.
- <=: Less than or Equal to Operator. It is used to compare if one value is less than or equal to another.
Here’s an example of comparison operator usage:
“`typescript
let num1 = 10;
let num2 = 5;
console.log(num1 == num2); // false
console.log(num1 != num2); // true
console.log(num1 > num2); // true
console.log(num1 < num2); // false
console.log(num1 >= num2); // true
console.log(num1 <= num2); // false
“`
Logical Operators
Logical operators are used to make logical statements. TypeScript provides logical operators such as:
- &&: Logical AND Operator. It is used to check if both conditions are true.
- ||: Logical OR Operator. It is used to check if at least one condition is true.
- !: Logical NOT Operator. It is used to reverse the logical state of an expression.
Here’s an example of logical operator usage:
“`typescript
let x = 10;
let y = 5;
console.log(x > 5 && y > 5); // false
console.log(x > 5 || y > 5); // true
console.log(!(x > 5)); // false
“`
Assignment Operators
Assignment operators are used to assign values to variables. TypeScript provides assignment operators such as:
- =: Simple Assignment Operator. It is used to assign a value to a variable.
- +=: Add and Assignment Operator. It is used to add a value to the existing value of a variable.
- -=: Subtract and Assignment Operator. It is used to subtract a value from the existing value of a variable.
- *=: Multiply and Assignment Operator. It is used to multiply a value with the existing value of a variable.
- /=: Divide and Assignment Operator. It is used to divide a value with the existing value of a variable.
- %=: Modulo and Assignment Operator. It is used to find the remainder of the division operation and assign it to the variable.
Here’s an example of assignment operator usage:
“`typescript
let num3 = 10;
num3 += 5;
console.log(num3); // 15
num3 -= 5;
console.log(num3); // 10
num3 *= 5;
console.log(num3); // 50
num3 /= 5;
console.log(num3); // 10
num3 %= 3;
console.log(num3); // 1
“`
Conditional Operator
Conditional operator is a ternary operator that is used to evaluate an expression based on a condition. The conditional operator is syntactically expressed using the question mark (?) and the colon (:). Here’s an example:
“`typescript
(condition) ? value1 : value2
“`
Here’s an example of conditional operator usage:
“`typescript
let age = 18;
let result = (age >= 18) ? “eligible” : “ineligible”;
console.log(result); // eligible
“`
FAQs
1. What are TypeScript operators?
TypeScript Operators are used to perform arithmetic or logical operations on variables and expressions.
2. What are arithmetic operators in TypeScript?
Arithmetic operators are used to perform mathematical operations on numeric data types. TypeScript provides arithmetic operators such as +, -, *, /, and %.
3. What are comparison operators in TypeScript?
Comparison operators are used to compare variables or expressions. TypeScript provides comparison operators such as ==, !=, >, <, >=, and <=.
4. What are logical operators in TypeScript?
Logical operators are used to make logical statements. TypeScript provides logical operators such as &&, ||, and !.
5. What are assignment operators in TypeScript?
Assignment operators are used to assign values to variables. TypeScript provides assignment operators such as =, +=, -=, *=, /=, and %=.
6. What is the conditional operator in TypeScript?
The conditional operator is a ternary operator that is used to evaluate an expression based on a condition. It is syntactically expressed using the question mark (?) and the colon (:).