TypeScript - Operators

An operator defines some function that will be performed on the data. The data on which operators work are called operands. Consider the following expression −
7 + 5 = 12
Here, the values 7, 5, and 12 are operands, while + and = are operators.
The major operators in TypeScript can be classified as −
Arithmetic operators
Logical operators
Relational operators
Bitwise operators
Assignment operators
Ternary/conditional operator
String operator
Type Operator
Assume the values in variables a and b are 10 and 5 respectively.
Show Examples
Relational Operators test or define the kind of relationship between two entities. Relational operators return a Boolean value, i.e., true/ false.
Assume the value of A is 10 and B is 20.
Show Examples
Logical Operators are used to combine two or more conditions. Logical operators too return a Boolean value. Assume the value of variable A is 10 and B is 20.
Show Examples
Assume variable A = 2 and B = 3
Show Examples
Show Examples
Note − Same logic applies to Bitwise operators, so they will become <<=, >>=, >>=, &=, |= and ^=.
Changes the sign of a value. Let’s take an example.
var x:number = 4 
var y = -x; 
console.log("value of x: ",x);   //outputs 4 
console.log("value of y: ",y);   //outputs -4

On compiling, it will generate following JavaScript code.
//Generated by typescript 1.8.10
var x = 4;
var y = -x;
console.log("value of x: ", x);   //outputs 4
console.log("value of y: ", y);   //outputs -4

It will produce the following output −
value of x:  4 
value of y:  -4

The + operator when applied to strings appends the second string to the first. The following example helps us to understand this concept.
var msg:string = "hello"+"world" 
console.log(msg)

On compiling, it will generate following JavaScript code.
//Generated by typescript 1.8.10
var msg = "hello" + "world";
console.log(msg);

It will produce the following output −
helloworld

The concatenation operation doesn’t add a space between strings. Multiple strings can be concatenated in a single statement.
This operator is used to represent a conditional expression. The conditional operator is also sometimes referred to as the ternary operator. The syntax is as given below −
Test ? expr1 : expr2

Test − refers to the conditional expression
Test − refers to the conditional expression
expr1 − value returned if the condition is true
expr1 − value returned if the condition is true
expr2 − value returned if the condition is false
expr2 − value returned if the condition is false
Let’s take a look at the following code −
var num:number = -2 
var result = num > 0 ?"positive":"non-positive" 
console.log(result)

Line 2 checks whether the value in the variable num is greater than zero. If num is set to a value greater than zero, it returns the string “positive” else the string “non-positive” is returned.
On compiling, it will generate following JavaScript code.
//Generated by typescript 1.8.10
var num = -2;
var result = num > 0 ? "positive" : "non-positive";
console.log(result);

The above code snippet will produce the following output −
non-positive

It is a unary operator. This operator returns the data type of the operand. Take a look at the following example −
var num = 12 
console.log(typeof num);   //output: number

On compiling, it will generate following JavaScript code.
//Generated by typescript 1.8.10
var num = 12;
console.log(typeof num);   //output: number

It will produce the following output −
number

This operator can be used to test if an object is of a specified type or not. The use of instanceof operator is discussed in the chapter classes.