Operators are symbols or signs that allow us to perform some operations on operands, such as addition, subtraction, or multiplication.
Typescript inherits most of its operators from Javascript, as they operate on values of various types.
Typescript supports various types of operators, including arithmetic, logical, comparison, assignment, and more.
Here's an overview of some commonly used operators in Typescript:
+ (addition): Adds two operands.
- (subtraction): Subtracts the right operand from the left operand.
* (multiplication): Multiple two operands.
/ (division): Divide two operands.
% (module): return the reminder of the division.
const num1: number = 10; const num2: number = 5; // Addition const addition = num1 + num2; // Subtraction const subtraction = num1 - num2; // Multiple const multiple = num1 * num2; // Division const division = num1 / num2; // Module const module = num1 % num2;
// == compare value only const doubleEqual = 5 == '5'; // true // === compare value and value type const tripleEqual = 5 === '5'; // false // <= const lessThanEqual = 10 <= 5; // false // >= const greaterThanEqual = 10 >= 5; // true // > const greaterThan = 10 > 5; // true // < const lessThan = 10 < 5; // false // != compare value only const notEqual = '5' != 5; // false // !== compare value and value type const notEqual = '5' !== 5; // true
== (equal to, with type coercion)
=== (equal to, without type coercion)
<= (less than or equal to)
>= (greater than or equal to)
> (greater than)
< (less than)
!= (not equal to, with type coercion)
!== (not equal to, without type coercion)
// AND Example: let andOperator = false && true; // false let orOperator = true || false; // true let notOperator = !false; // true
const num = 10; let isNumberDivisibleByTwo = (num % 2 === 0) ? "Yes" : "No";
&& (logical AND): Returns `true` if nth operands are `true`, else return `false`
|| (logical OR): Returns `true` if any of the operands is `true`, else return `false`
! (logical NOT): it revert the value.
condition ? "Yes": "No" (returns "Yes" if condition is match, otherwise "No")