TypeScript Operators

Operators are used to perform operations on program code, operating on one or more operands.

Consider the following calculation:

7 + 5 = 12

In the above example, 7, 5, and 12 are operands.

The operator+is used for addition.

The operator=is used for assignment.

TypeScript mainly includes the following types of operations:

  • Arithmetic Operators
  • Logical Operators
  • Relational Operators
  • Bitwise Operators
  • Assignment Operators
  • Ternary/Conditional Operators
  • String Operators
  • Type Operators

Arithmetic Operators

Assumey=5, the following table explains the operations of these arithmetic operators:

Operator Description Example x Result y Result
+ Addition x=y+2 7 5
- Subtraction x=y-2 3 5
* Multiplication x=y*2 10 5
/ Division x=y/2 2.5 5
% Modulus (remainder) x=y%2 1 5
++ Increment x=++y 6 6
x=y++ 5 6
-- Decrement x=--y 4 4
x=y-- 5 4

Example

var num1:number = 10
var num2:number = 2
var res:number = 0
   
res = num1 + num2
console.log("Addition: "+res);

res = num1 - num2;
console.log("Subtraction: "+res)

res = num1*num2
console.log("Multiplication: "+res)

res = num1/num2
console.log("Division: "+res)
   
res = num1%num2
console.log("Remainder: "+res)

num1++
console.log("num1 increment operation: "+num1)

num2--
console.log("num2 decrement operation: "+num2)

Use thetsctsc command to compile the above code to get the following JavaScript code:

var num1 = 10; var num2 = 2; var res = 0; res = num1 + num2; console.log("Addition:" + res); res = num1 - num2; console.log("Subtraction:" + res); res = num1 * num2; console.log("Multiplication:" + res); res = num1 / num2; console.log("Division:" + res); res = num1 % num2; console.log("Remainder:" + res); num1++; console.log("num1 increment operation:" + num1); num2--; console.log("num2 decrement operation:" + num2);

Executing the above JavaScript code produces the following output:

加:        12
减: 8
乘:    20
除:   5
余数:   0
num1 自增运算: 11
num2 自减运算: 1

Relational Operators

Relational operators are used to calculate whether the result is true or false.

x=5, the following table explains the operations of the relational operators:

Operator Description Comparison Return value
== Equal to x==8 false
x==5 true
!= Not equal to x!=8 true
> Greater than x>8 false
< Less than x<8 true
>= Greater than or equal to x>=8 false
<= Less than or equal to x<=8 true

Example

var num1:number = 5; var num2:number = 9; console.log("The value of num1 is:"+num1); console.log("The value of num2 is:"+num2); var res = num1>num2 console.log("num1 is greater than num2:"+res) res = num1<num2 console.log("num1 is less than num2:"+res) res = num1>=num2 console.log("num1 is greater than or equal to num2:"+res) res = num1<=num2 console.log("num1 is less than or equal to num2:"+res) res = num1==num2 console.log("num1 equals num2:"+res) res = num1!=num2 console.log("num1 is not equal to num2:"+res)

Use thetsctsc command to compile the above code to get the following JavaScript code:

var num1 = 5; var num2 = 9; console.log("The value of num1 is:" + num1); console.log("The value of num2 is:" + num2); var res = num1 > num2; console.log("num1 is greater than num2:" + res); res = num1 < num2; console.log("num1 is less than num2:" + res); res = num1 >= num2; console.log("num1 is greater than or equal to num2:" + res); res = num1 <= num2; console.log("num1 is less than or equal to num2:" + res); res = num1 == num2; console.log("num1 equals num2:" + res); res = num1 != num2; console.log("num1 is not equal to num2:" + res);

Executing the above JavaScript code produces the following output:

num1 的值为: 5
num2 的值为:9
num1 大于n num2: false
num1 小于 num2: true
num1 大于或等于  num2: false
num1 小于或等于 num2: true
num1 等于 num2: false
num1 不等于 num2: true


Logical Operators

Logical operators are used to determine the logic between variables or values.

Given x=6 and y=3, the following table explains the logical operators:

Operator Description Example
&& and (x < 10 && y > 1) is true
|| or (x==5 || y==5) is false
! not !(x==y) is true

Example

var avg:number = 20; var percentage:number = 90; console.log("avg value is:"+avg+",percentage value is:"+percentage); var res:boolean = ((avg>50)&&(percentage>80)); console.log("(avg>50)&&(percentage>80): ",res); var res:boolean = ((avg>50)||(percentage>80)); console.log("(avg>50)||(percentage>80): ",res); var res:boolean=!((avg>50)&&(percentage>80)); console.log("!((avg>50)&&(percentage>80)): ",res);

Use thetsctsc command to compile the above code to get the following JavaScript code:

var avg = 20; var percentage = 90; console.log("avg value is:" + avg + ",percentage value is:" + percentage); var res = ((avg > 50) && (percentage > 80)); console.log("(avg>50)&&(percentage>80): ", res); var res = ((avg > 50) || (percentage > 80)); console.log("(avg>50)||(percentage>80): ", res); var res = !((avg > 50) && (percentage > 80)); console.log("!((avg>50)&&(percentage>80)): ", res);

Executing the above JavaScript code produces the following output:

avg 值为: 20 ,percentage 值为: 90
(avg>50)&&(percentage>80):  false
(avg>50)||(percentage>80):  true
!((avg>50)&&(percentage>80)):  true

Short-circuit Operators (&& and ||)

The && and || operators can be used to combine expressions. The && operator returns true only when both the left and right expressions are true.

Consider the following example:

var a = 10 
var result = ( a<10 && a>5)

In the above example, a < 10 and a > 5 are a combined expression using the && operator. The first expression returns false. Since the && operation requires both expressions to be true, if the first one is false, the subsequent evaluation (a > 5 is skipped) is not executed, and false is returned directly.

The || operator returns true for the combined expression as long as one of the expressions is true.

Consider the following example:

var a = 10 
var result = ( a>5 || a<10)

In the above example, a > 5 and a < 10 are a combined expression using the || operator. The first expression returns true. Since the || combined operation only requires one expression to be true, if the first one is true, the subsequent evaluation (a < 10 is skipped) is not executed, and true is returned directly.


Bitwise Operators

Bitwise operations are unary and binary operations on bit patterns or binary numbers in programming.


Operator Description Example Similar to Result Decimal
& AND. Bitwise AND processes two binary numbers of the same length. If both corresponding bits are 1, the result bit is 1; otherwise, it is 0. x = 5 & 1 0101 & 0001 0001  1
| OR. Bitwise OR processes two binary numbers of the same length. If at least one of the corresponding bits is 1, the result bit is 1. x = 5 | 1 0101 | 0001 0101  5
~ NOT. Negation is a unary operator that performs logical negation on each bit of a binary number, turning 1 into 0 and 0 into 1. x = ~ 5  ~0101 1010  -6
^ XOR. Bitwise XOR performs a logical XOR operation on each bit of equal-length binary patterns or binary numbers. If the bits differ, the result bit is 1; otherwise, it is 0. x = 5 ^ 1 0101 ^ 0001 0100  4
<< Left shift. Shifts all bits of the operand to the left of << to the left by the number of bits specified by the operand to the right of <<. High-order bits are discarded and low-order bits are filled with 0. x = 5 << 1 0101 << 1 1010  10
>> Right shift. Shifts all bits of the operand to the left of >> to the right by the number of bits specified by the operand to the right of >>. x = 5 >> 1 0101 >> 1 0010 2
>>> Unsigned right shift. Similar to signed right shift, except that the left side is always filled with 0. x = 2 >>> 1 0010 >>> 1 0001 1

Example

var a:number = 2; //Binary 10 var b:number = 3; //Binary 11 var result; result = (a & b); console.log("(a & b) => ",result) result = (a | b); console.log("(a | b) => ",result) result = (a ^ b); console.log("(a ^ b) => ",result); result = (~b); console.log("(~b) => ",result); result = (a << b); console.log("(a << b) => ",result); result = (a >> b); console.log("(a >> b) => ",result); result = (a >>> 1); console.log("(a >>> 1) => ",result);

Use thetsctsc command to compile the above code to get the following JavaScript code:

var a = 2; //Binary 10 var b = 3; //Binary 11 var result; result = (a & b); console.log("(a & b) => ", result); result = (a | b); console.log("(a | b) => ", result); result = (a ^ b); console.log("(a ^ b) => ", result); result = (~b); console.log("(~b) => ", result); result = (a << b); console.log("(a << b) => ", result); result = (a >> b); console.log("(a >> b) => ", result); result = (a >>> 1); console.log("(a >>> 1) => ", result);

Executing the above JavaScript code produces the following output:

(a & b) =>  2
(a | b) =>  3
(a ^ b) =>  1
(~b) =>  -4
(a << b) =>  16
(a >> b) =>  0
(a >>> 1) =>  1

Assignment Operators

Assignment operators are used to assign values to variables.

Given x=10x=10 and y=5, the following table explains the assignment operators:

Operator Example Equivalent x value
= (assignment) x = y x = y x = 5
+= (add and assign) x += y x = x + y x = 15
-= (subtract and assign) x -= y x = x - y x = 5
*= (multiply and assign) x *= y x = x * y x = 50
/= (divide and assign) x /= y x = x / y x = 2

Similarly, logical operators can also be used in combination with assignment operators:<<=, >>=, >>>=, &=, |=and^=。

Example

var a: number = 12 var b:number = 10 a = b console.log("a = b: "+a) a += b console.log("a+=b: "+a) a -= b console.log("a-=b: "+a) a *= b console.log("a*=b: "+a) a /= b console.log("a/=b: "+a) a %= b console.log("a%=b: "+a)

Use thetsctsc command to compile the above code to get the following JavaScript code:

var a = 12; var b = 10; a = b; console.log("a = b: " + a); a += b; console.log("a+=b: " + a); a -= b; console.log("a-=b: " + a); a *= b; console.log("a*=b: " + a); a /= b; console.log("a/=b: " + a); a %= b; console.log("a%=b: " + a);

Execute the above JavaScript code, the output result is:

a = b: 10
a+=b: 20
a-=b: 10
a*=b: 100
a/=b: 10
a%=b: 0

Ternary Operator (?)

The ternary operation has 3 operands and needs to evaluate the value of a boolean expression. The main purpose of this operator is to decide which value should be assigned to a variable.

Test ? expr1 : expr2
  • Test − The specified conditional statement
  • expr1 − The value returned if the conditional statement Test evaluates to true
  • expr2 − The value returned if the conditional statement Test evaluates to false

Let's look at the following example:

var num:number = -2 var result = num > 0 ? "Greater than 0" : "Less than 0, or equal to 0" console.log(result)

The example is used to determine whether the variable is greater than 0.

Use the tsc command to compile the above code and get the following JavaScript code:

var num = -2; var result = num > 0 ? "Greater than 0" : "Less than 0, or equal to 0"; console.log(result);

The output of the above example is as follows:

小于 0,或等于 0

Type Operators

typeof Operator

typeof is a unary operator that returns the data type of the operand.

See the following example:

var num = 12 console.log(typeof num); //Output result: number

Use the tsc command to compile the above code and get the following JavaScript code:

var num = 12; console.log(typeof num); //Output result: number

The output of the above example is as follows:

number

instanceof

The instanceof operator is used to determine whether an object is of a specified type. We will introduce it in detail in later chapters.


Other Operators

Unary Negation Operator (-)

Changes the sign of the operand. See the following example:

var x:number = 4 var y = -x; console.log("x value is:",x); //Output result 4 console.log("y value is:",y); //Output result -4

Use the tsc command to compile the above code and get the following JavaScript code:

var x = 4; var y = -x; console.log("x value is:", x); //Output result 4 console.log("y value is:", y); //Output result -4

The output of the above example is as follows:

x 值为:  4
y 值为:  -4

String Operators: Concatenation Operator (+)

The + operator can concatenate two strings. See the following example:

var msg:string = "EXAMPLE"+".COM" console.log(msg)

Use the tsc command to compile the above code and get the following JavaScript code:

var msg = "EXAMPLE" + ".COM"; console.log(msg);

The output of the above example is as follows:

example.com
Other extensions