Operators in TypeScript

We perform a function on a piece of data using operators. This piece of data on which a function is performed is called operand(s).

There are various operators divided into multiple categories:

Arithmetic Operators

Arithmetic operators are used to perform operations on numerical operands. Below are some arithmetic operators with example. Let’s take two variables: a=10, b=20.

Operator Description Example
+ Addition. This will return the sum of two operands a + b = 30
Subtraction. This will return the difference between two operands b-a=10
* Multiplication. This will return the product of two operands a*b=200
/ Division. This will return quotient b/a=2
% Modulus. This will return remainder b%a=0
++ Increment. This will increment the value of operand by one a++ will return 11
Decrement. This will decrement the value of operand by one a– will return 9
Relational Operators

These operators define the relationship between two operands and returns true or false. Below are some relational operators with example. Let’s take two variables: a=10, b=20.

Operator Description Example
> Greater Than (b > a) will return true
< Less Than (a < b) will return true
>= Greater than or equal to (a >= b) will return false
<= Less than or equal to (a <= b) will return true
== Equal To (a == b) will return false
!= Not Equal to (a != b) will return true
Logical Operators

These operators are used to combine two or more conditions. They also return boolean value, true or false. Below are some logical operators with example. Let’s take two variables: a=10, b=20.

Operator Description Example
&& AND. This will return true if all the conditions are true ((a < b) && (a != b)) will return true as both these conditions are true
|| OR. This will return true if any one of the condition is true ((a < b) && (a == b)) will return true as first condition is true
! NOT. This will return the opposite of the condition result (!(a < b)) will return false.
Assignment Operators

These operators are used to assign values to a variable. Below are some assignment operators with example. Let’s take two variables: a=10, b=20.

Operator Description Example
= Assignment. Assigns the value of right side operand to left side operand b = a (This will assign 10 to variable b)
+= Add and Assign. Add value of right operand to left operand and then assigns the sum to left operand a += b (Adds 20 to 10 and then assigns sum(30) to variable a)
-= Subtract and Assign. Subtract right operand from left operand and then assigns the difference to left operand b -= a (Subtracts 10 from 20 and assigns the difference(10) to variable b)
*= Multiply and Assign. Multiply right operand to left operand then assigns the product to left operand. a *= b (Multiplies a and b and assigns the product(200) to a)
/= Divide and Assign. Divides left operand with right operand and assigns quotient to left operand b /= a (Divides b by a and assigns quotient(2) to b)
Negation Operator (-)

It will change the sign of the integer value. That is, it will change positive value to negative value and negative value to positive value.

var num1:number = 10;
var num2 = -num1;
console.log("num1: " + num1 + "; num2: " + num2);
Output: num1: 10; num2: -10

After executing, num1 will be 10 and num2 will be -10.

Concatenation Operator (+)

This operator is applied on string values to concatenate two strings together. We can concatenate multiple strings in one sentence using multiple concatenation operators between two strings.

var fullName:string="Hello" + " Akanksha" + " Garg";
console.log(fullName);
Output: Hello Akanksha Garg

Kindly note that concatenation operator does not add a space between two strings, you have to explicitly place a space like I did in my example.

typeof Operator

This operator returns the data type of a variable.

var firstname="Akanksha";
console.log(typeof(firstname));
Output: string
Unknown's avatar

Author: Akanksha Gupta

I am a developer and working on SharePoint and Project Server in an MNC. I have more than 10 years of experience in the same field.

Leave a comment

Design a site like this with WordPress.com
Get started