7.TypeScript Operators - Tutorial Rays
TypeScript

7.TypeScript Operators

7.TypeScript Operators

What is an Oper=j+9WQator? 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 * Logical operators * Relational operators * Assignment operators * Ternary/conditional operator * String operator * Type Operator Arithmetic Operators 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 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 Logical Operators 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

Assignment Operators 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 ^=. Miscellaneous Operators The negation operator (-) Changes the sign of a value. Let’s take an example.

var a:number = 10
var b:number = -a;
console.log("value of a: ",a);   //outputs 10
console.log("value of b: ",b);   //outputs -10

On compiling, it will generate following JavaScript code. String Operators When + sign is applied to between two strings then it concatenate two strings and shows as a single string.

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

Conditional Or Ternary Operator (?) 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.

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);

Type Operators 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 −

var num = 300
console.log(typeof num);

It will produce the following output − number

Leave a Reply

Your email address will not be published. Required fields are marked *