Table of contents
1.
Introduction 
2.
Arithmetic operators
3.
Logical operators
4.
Relational operators
5.
Bitwise operators
6.
Assignment operators
7.
Conditional operator
8.
Type Operator
9.
FAQs
10.
Key Takeaways
Last Updated: Mar 27, 2024

Typescript Operators

Author Parth Jain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

In a calculator, we operate with two or more numbers by adding, subtracting, multiplying, or dividing the operands (numbers). The goal is to operate on the numbers to obtain a specific result. It is crucial to understand the fundamental operators provided to perform operations in programming.

An operator is a symbol that is responsible for performing operations.

As an example, let us consider the expression,

5+20=25 

In this case, the numbers 5 and 20 are operands, and the symbols and are called operators. The + operator is one of the many operators available in Typescript.

Let us dive deeper into the types of operators:

  • Arithmetic operators
  • Logical operators
  • Relational operators
  • Bitwise operators
  • Assignment operators
  • Ternary/conditional operator
  • Type Operator

Arithmetic operators

The arithmetic operator is responsible for performing arithmetical operations. It takes numerical values as input, operates on them, and returns the output as a single value

Operator Name  Description

+

Addition operator

It takes two or more operands and performs addition. If the given arguments are of type string, it concatenates them instead.

//Addition

let num1=23;

let num2=2

let sum = num1+num2;

console.log(sum)                                    //25;

//String concatenation

let str1 ="Hello"

let str2 = "Typescript"

let add = str1+ " "+str2

console.log(add)                                    //"Hello Typescript"

-

Subtraction operator.

 

It takes two or more operands and performs subtraction. 

//Subtraction

let num1=23;

let num2=2

let diff= num1-num2;

console.log(diff)                                         //21;

/

Division operator.

It takes two operands as numerator and denominator, then performs division. 

//Division

let num=40;

let den=4;

console.log(num/den)                                   //10;

*

Multiplication operator.

 

It takes two or more operands and performs Multiplication.

//Multiplication

let num1=2;

let num2=4;

let mult= num1*num2;

console.log(mult)                                    //8;

%

Remainder operator

 

It is also known as modulus operator and is responsible for performing division then returning the remainder.

//Remainder

let num1=5;

let num2=4;

let remainder= num1%num2;

console.log(remainder);                   //1

**

Exponentiation operator.

 

It takes two operands and raises the power of the left operand by the value of the right operand.

//Exponent

let num1=2;

let num2=3;

let ans= num1**num2;

console.log(ans);                         //2*2*2=8

++

Increment operator

 

It takes a single operand and increases its value by 1.

let num=2;

console.log(num);             //2

num++;

console.log(num);             //3

++num; 

console.log(num);             //4

--

Decrement operator

 

It takes a single operand and decreases its value by 1.

let num=10;

console.log(num);             //10

num--

console.log(num);             //9

--num; 

console.log(num);             //8

 

Logical operators

The logical operator is responsible for performing logical operations. It implements logical conditions on two or more operands and returns either True or False. 

Operator Name  Description

&&

Logical AND

 

The logical AND operator returns True only if all the given conditions return True. If the first condition is False, no further checks are performed, and the returned output is False. 

let num1=50;

let num2=10

console.log(num1 > num2 && num2 > 9)       //true   

||

Logical OR

 

The logical OR operator returns False only if all the given conditions return False. In other words, if one of the given conditions returns True, the entire condition is considered True. 

let num1=50;

let num2=10

console.log(num1 > num2 || num2 > 9)       //true   

!

Logical NOT

 

The logical NOT operator returns the opposite of the entered boolean value. 

True is returned as False and vice versa.

console.log(!true); //false

 

Relational operators

The relational operator defines a relation between two or more operands. After performing relational operations, these operators return either true or false Boolean values.

Let us assume that A’s value is 5 and B’s is 15. 

Operator  Description

>

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

 

Bitwise operators

The Bitwise operator is responsible for performing Bit-related computations. As every number in programming is composed of a set of 0s and 1s, the actions performed with these bits is possible with Bitwise Operators.,

Let us assume the variable A = 2 and B = 3

LSB=Least Significant Bit

MSB=Most Significant Bit

Operator Description Example

& (Bitwise AND)

It takes two operands and performs Logical AND operation on each operand bit from the right(LSB) to the left (MSB). (A & B) is 2
    | (BitWise OR) It takes two operands and performs a logical OR operation on each operand bit from the right(LSB) to the left (MSB). (A | B) is 3
    ^ (Bitwise XOR)

It takes two operands and performs Boolean XOR operation on each operand bit from the right(LSB) to the left (MSB).

Meaning, If both bits are the same, it returns 0 else 1

(A ^ B) is 1
    ~ (Bitwise Not)

It takes one operand and swaps each bit from 0 to 1 and 1 to 0.

Input=0110

Output=1001

(~B) is -4
<< (Left Shift) It takes two operands and shifts the bits of the first operand to the left by the value of the right operand. (A << 1) is 4
>> (Right Shift) It takes two operands and shifts the bits of the first operand to the right by the value of the right operand. (A >> 1) is 1

Read about Bitwise Operators in C here.

Assignment operators

The assignment operator is responsible for assigning values to a variable. 

Operator Description
= (Simple Assignment)

Assigns values from the right of = to the left side

C= A + B;

The sum of A and B is stored in C 

+= (Add and Assignment)

It assigns the sum of left and right operands to the left operand.

B+=A;

Means, B=A+B;

The sum of A and B is stored in B.

-= (Subtract and Assignment)

It assigns the difference of left and right operands to the left operand.

B-=A;

Means, B= B - A;

The difference between B and A is stored in B.

*= (Multiply and Assignment)

It assigns the Multiplication of left and right operands to the left operand.

B*=A;

Means, B=A*B;

The Multiplication of A and B is stored in B.

/= (Divide and Assignment)

It assigns the division result of left and right operands to the left operand.

B/=A;

Means, B=B/A;

The division of B and A is stored in B.

 

Conditional operator

The conditional operator is beneficial as it drastically reduces the code line. 

Instead of using an if-else statement, we can use Conditional operator syntax.

Syntax

condition ? expression1 : expression2;

If the given condition is true,expression1 is executed. Else,expression2 executes.

let isAdult = true;
let message = isValid ? 'Welcome' : 'Underage please exit';
console.log(message)                   //'Welcome”

Type Operator

The type operator returns the data type of the input variable. 

console.log(typeof 60) // number
console.log(typeof "Ninja") // string
console.log(typeof true) // boolean
console.log(typeof Math.round) // function
console.log(typeof undefined) // undefined
console.log(typeof null) // object

FAQs

1. What is a TypeScript operator?
An operator is a symbol that uses operands as inputs and performs operations. Then, it returns a value that is either a number or boolean. All Javascript operators are present in Typescript.

2. What is Nullish coalescing operator?
The Nullish coalescing operator is a logical operator that returns the right operand in case the given left operand is undefined. Otherwise, it returns the left operand. It is represented by the symbol (??)

 

Key Takeaways

From this article, we learned about Typescript's various operators and how their syntaxes are used. We also learned the key differences between the logical and bitwise operators. We looked into how conditional operators make writing if-else statements simple.

However, this isn't enough, as there is always much more to explore and learn about this vast field of Web Development. To know more about Typescript and its intricacies, check out the articles on Typescript or enroll in our highly curated Web Development course.

To learn more about Micro Operations, refer to Arithmetic Micro Operations.

Live masterclass