Follow us on Facebook
Faucibus toroot menuts
TypeScript Operators
Operators are symbol or characters which defines some action that will be performed on the data. The data can be variable , function or simple two values. Suppose I am taking a variable called ‘a’ and then I assign the value 10.
var a:number = 10;
In the above statement , ‘=’ is operator and ‘a’ is an operand.
There are following few of operators which is supported by TypeScript
Arithmetic operators are normally those which is used to simple calculation. Generally numerical calculation.
Operator |
Description |
Example |
+ |
For adding two or more numbers |
a + b |
- |
For subtracting from one number to another |
a - b |
* |
For multiplying two or more values |
a * b |
/ |
For performing division |
a / b |
% (Modulus) |
To get remainder after division |
a % b |
++ |
For self increment |
a++ |
-- |
For self decrement |
a-- is |
Relational operators are those which shows relation between two operands. Generally comparison is one between two values and boolean value is returned after evaluating the two values.
Operator |
Description |
Example |
> |
Greater than |
(A > B) is False |
< |
Lesser than |
(A < B) is True |
>= |
Greater than or equal to |
(A >= B) is False |
<= |
Lesser than or equal to |
(A <= B) is True |
== |
Equality |
(A == B) is false |
!= |
Not equal |
(A != B) is True |
Under these kind of operators two or more value is evaluated. If both the values are true then result true. If anyone of the values are then ture .
Operator |
Description |
Example |
&& (And) |
It checks if both the conditions are true or not. Both condition fulfills the result is true. |
(a > 5 && B > 5) is False |
|| (OR) |
If any one of the conditions are fulfilled then the result is true. |
(A > 5 || B >5 is True |
! (NOT) |
If the condition is true then it returns false and if condition is false then it returns true. |
!(A >10 ) is True |
Under this operator, certain action is performed and the the value is assigned.
Operator |
Description |
Example |
= |
Right side value is assigned to left side |
Var a:number=10 |
+= |
It adds the right operand to the left operand and assigns the result to the left operand. |
C += A is equivalent to C = C + A |
-= (Subtract and Assignment) |
It subtracts the right operand from the left operand and assigns the result to the left operand. |
C -= A is equivalent to C = C - A |
*= (Multiply and Assignment) |
It multiplies the right operand with the left operand and assigns the result to the left operand. |
C *= A is equivalent to C = C * A |
/= (Divide and Assignment) |
It divides the left operand with the right operand and assigns the result to the left operand. |
Note − Same logic applies to Bitwise operators, so they will become <<=, >>=, >>=, &=, |= and ^=.
Changes the sign of a value. Let’s take an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 | var a:number = 10 var b = -x; console.log("value of x: ",a); //outputs 10 console.log("value of y: ",b); //outputs -10 On compiling, it will generate following JavaScript code. //Generated by typescript 1.8.10 var a = 10; var b = -10; console.log("value of x: ", a); //outputs 10 console.log("value of y: ", b); //outputs -10 It will produce the following output − value of a: 10 value of b: -10 |
1 2 3 4 5 6 7 8 9 10 11 12 | var msg:string = "Rajesh"+"Kumar" console.log(msg) On compiling, it will generate following JavaScript code. //Generated by typescript 1.8.10 var msg = "Rajesh" + "Kumar"; console.log(msg); It will produce the following output − RajeshKumar |
This is one of the conditional operator . This is also called Ternary Operator.
evaluation? expr1 : expr2
Example : a>b ? a:b
Evaluation− In this, conditional expression is evaluated.
expr1 − value returned if the condition is true
expr2 − value returned if the condition is false
In the above case first, it checked if a is greater than b then value of a is returned or else b is returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | var a:number = 2 var b:number = 4 var c:number = a>b ? a : b console.log(c); On compiling, it will generate following JavaScript code. //Generated by typescript 1.8.10 var a = 2; var b = 4; var c = a > b ? a : b; console.log(c); |
If we want to know the datatype of any variable/operand , then typeof Operator is used.
the operand. Take a look at the following example −
1 2 | var num = 300 console.log(typeof num); |
It will produce the following output −
number