TypeScript Operators

Learn via video courses
Topics Covered

Overview

An operator is a special symbol that operates on operands by defining a function that is called upon whenever the operator is invoked. Each operator has a unique symbol used to invoke that operator. Since TypeScript is a strongly typed superset of JavaScript, TypeScript supports all the standard operators supported by JavaScript.

Types of Operators Based on Number of Operands

The operator operates on operands. There are 3 types of operators based on the number of operands :

  1. Unary operator :
    These operators require only one operand.

    Example :

  2. Binary operator :
    These operators require two operands. Most operators that are available in TypeScript are binary operators.

    Example :

  3. Ternary operator :
    These operators require three operands where the first operand is conditional. TypeScript has a ternary operator ?

    Example :

Therefore, all typescript operators can be classified as one of the three : unary, binary, or ternary based on the number of operands it receives.

Introduction

TypeScript operators can be classified into one of the following categories :

  1. Arithmetic operators
  2. Relational/Comparision operators
  3. Logical operators
  4. Bitwise operators
  5. Assignment operators
  6. Ternary/Conditional operators
  7. Concatenation operators
  8. Type operators

Below we will see all the operators that are present in TypeScript along with some examples.

Operators in TypeScript

1. Arithmetic Operators

Operators who take numeric values as operand(s) and perform some action on the operands to return a single numeric value are generally placed in the category of Arithmetic operators.

In TypeScript, there are 7 arithmetic operators :

  • + operator or Addition operator :

    Example :

    Output :

  • - operator or Subtraction operator :

    Example :

    Output :

  • / operator or Division operator :

    Example :

    Output :

  • * operator or Multiplication operator :

    Example :

    Output :

  • % operator or Modulus operator :

    Example :

    Output :

  • ++ operator or Increment operator :

    Example :

    Output :

  • -- operator or Decrement operator :

    Example :

    Output :

2. Comparison (Relational) Operators

Operators which are used to compare or to define the relationship between two operands and return a boolean value (True or False) are generally placed in the category of Comparision / Relational operators.

In TypeScript, there are 8 relational / comparision operators :

  • == operator or Is equal to the operator :

    Example :

    Output :

  • === operator or Is identical to the operator :

    Example :

    Output :

  • != operator or Not equal to the operator :

    Example :

    Output :

  • !== operator or Not identical to the operator :

    Example :

    Output :

  • > operator or Greater than operator :

    Example :

    Output :

  • >= operator or Greater than or equal to the operator :

    Example :

    Output :

  • < operator or Less than operator :

    Example :

    Output :

  • <= operator or Less than or equal to the operator :

    Example :

    Output :

3. Logical Operators

Operators which are used to combine two or more conditions into a single expression that returns a boolean value (True or False) are generally placed in the category of Logical operators.

In TypeScript, there are 3 logical operators :

  • && operator or Logical AND operator :

    Example :

    Output :

  • || operator or Logical OR operator :

    Example :

    Output :

  • ! operator or Logical NOT operator :

    Example :

    Output :

4. Bitwise Operators

Operators which are used to perform bit manipulation and bitwise operations on operand(s) are generally placed in the category of Bitwise operators :

In TypeScript, there are 7 bitwise operators :

  • & operator or Bitwise AND operator :

    Example :

    Output :

  • | operator or Bitwise OR operator :

    Example :

    Output :

  • ^ operator or Bitwise XOR operator :

    Example :

    Output :

  • ~ operator or Bitwise NOT operator :

    Example :

    Output :

  • >> operator or Bitwise Right Shift operator :

    Example :

    Output :

  • << operator or Bitwise Left Shift operator :

    Example :

    Output :

  • >>> operator or Bitwise Right Shift With Zero operator :

    Example :

    Output :

5. Assignment Operators

Operators which are used to assign values to the operands or variables are generally placed in the category of Assignment operators.

In TypeScript, there are 6 Assignment operators :

  • = operator or Assign operator :

    Example :

  • += operator or Add and assign operator :

    Example :

    Output :

  • -= operator or Subtract and assign operator :

    Example :

    Output :

  • *= operator or Multiply and assign operator :

    Example :

    Output :

  • /= operator or Divide and assign operator :

    Example :

    Output :

  • %= operator or Modulus and assign operator :

    Example :

    Output :

6. Ternary/conditional Operator

  • This operator requires three operands and returns a boolean value based on the condition.

  • It is a shorthand way of writing an if-else statement

  • here, expression is the conditional statement and based on its value either expression1 (when expression is True) will be executed or expression2 (when expression is False)

    Example :

    Output :

7. Concatenation Operator

The concatenation operator is used to join two or more strings into one single string.

Example :

8. Type Operator

Operators which are used with objects or assist in working with objects such as typeof, instanceof, in, or delete are generally placed in the category of Type operators.

In TypeScript, there are 4 type operators :

  • in operator :

    Example :

    Output :

  • typeof operator :

    Example :

    Output :

  • instanceof operator :

    Example :

    Output :

  • delete operator :

    Example :

Conclusion

  • TypeScript supports all the standard operators supported by JavaScript
  • TypeScript operators can be generally divided into the following categories: Arithmetic, Logical, Bitwise, Relational, Assignment, Ternary, Concatenation, and Type Operators.
  • there are 7 arithmetic operators in TypeScript : +, -, /, *, %, ++, and --
  • there are 8 relational operators in TypeScript : >, <, >=, <=, ==, === and !=, !==
  • there are 3 logical operators in TypeScript : &&, ||, and !
  • there are 7 bitwise operators in TypeScript : &, |, ^, ~, >>, << and >>>
  • there are 6 assignment operators in TypeScript : =, +=, -=, *=, /=, and %=.