TypeScript Operators

Operators is an special symbols, which are used to compare, assignment, and perform logical and mathematical operation. That is a simple way to describe the role of an operator. But that is a basic level, operator is much useful. That are used basic and special purpose.

Arithmetic Operator

Arithmetic operator are work in two operands, that is also know as binary operators. We can thinking about this type of operators. This operator are works two operands such as left and right. And operator are exist in between of them. operator is decide to which operation are performed to those operand. In given table mention of all arithmetic operators.

Operator Name
+ Addition(+)
- Subtract (-)
* Multiplication (*)
/ Divide
% Remainder operator

var a : number = 10;
var b : number = 3;
// Addition
var result = a + b; // 13
console.log(a + " + " + b + "  = " + (result));
// Subtract
result = a - b; // 7
console.log(a + " - " + b + "  = " + (result));
// Multiplication
result = a * b; // 30
console.log(a + " * " + b + "  = " + (result));
// Divide
result = a / b; // 3.3333333333333335
console.log(a + " / " + b + "  = " + (result));
// Remainder operator
result = a % b; // 1
console.log(a + " % " + b + "  = " + (result));

Logical Operator

Operator Name
&& Logical AND operator.
|| Logical OR Operator
! Logical NOT Operator

Logical operator are work in boolean values. && operator are used to check expression condition when both left and right the expression are produced boolean true value. Then that are produce an true boolean result. Otherwise produce false. In given list are mentioned logical operators.

// Example logic && operator
var result = true && true;
console.log(result);//true
result = false && false;
console.log(result);//false
result = true && false;
console.log(result);//false
result = false && true;
console.log(result);//false

Logical Or || is an binary operator, That is work on boolean values. This operator are produce true value when any one expression are produced true result (left and right operand result).

// Example logic || operator
var result = true || true;
console.log(result); //true
result = false || false;
console.log(result); //false
result = true || false;
console.log(result); //true
result = false || true;
console.log(result); //true

Logical Not ! is an unary operator that are work on boolean value. When expression is produce true value that is modified this result to false. And when expression is produce false boolean value then that is change this result to true boolean value.

 // Example logic ! operator
var trueStatus  : boolean = true;
var falseStatus : boolean  = !trueStatus;
//use logical not
console.log(trueStatus);  // true
console.log(falseStatus); // false

Bitwise Operators

Bitwise operator is based on binary value and that are work on two operands. Normally all operators are produce boolean values but that is work on a binary bit level modification. In given table mentioned all Bitwise operator.

Operator Name
& Binary AND operator.
| Binary OR Operator
^ Binary XOR Operator
~ Binary Ones Complement Operator
<< Binary Left Shift Operator
>> Binary Right Shift Operator
// Example of Binary And Operator
/*
    *  Binary of 5 =   0  1  0   1
    *  Binary of 4 =   0  1  0   0
    *            &
    *--------------------------------
    *     Result 4 =   0  1  0   0
*/
var a : number = 5 & 4;
console.log(a); // 4
// Example of Binary OR Operator
/*
    *  Binary of 3 =   0  0  1   1
    *  Binary of 4 =   0  1  0   0
    *            |
    *--------------------------------
    *     Result 7 =   0  1  1   1
*/
var a = 3 | 4;
console.log(a); // 7
// Example of Binary XOR Operator
/* 
    Binary of 5 =   0  1  0   1
    Binary of 4 =   0  1  0   0
                ^   
    --------------------------------
       Result 1 =   0  0  0   1
    ----------------------------------
    When  0  1  = 1
    When  0  0  = 0
    When  1  1  = 0
    (both bits are different then work)
*/
var a = 5 ^ 4;
console.log(a); // 1
// Example of Complement Operator

var testOne : number = 15;
var testTwo : number = -6;
/*
    Bitwise 1s Complement Operator
    Example 1:
      15   = 1  1  1  1 (binary of 15)
      add            1 (binary of 1)
    +   
    --------------------------------
    Result = 1 0  0  0  0 (binary of 16)
    --------------------------------- 
    output = - Result
    output = - 16

    Example 2: first remove that (- sign)
      6    = 0  1  1  0 (binary of  6)
      minus           1 (binary of  1)
    -                  (Perform minus)
    --------------------------------
    Result =  0  1  0  1 (binary of 5)
    Result = 5 
    // add remove sign
    Result = -5 
    --------------------------------- 
    // multiply by (- sign)
    output = - Result
    output = - (-5)
    output = 5
        
*/
var result : number = ~testOne;
console.log(result); // -16
result = ~testTwo;
console.log(result); // 5
// Left shift example
// number 15
// binary (1111)
// shift by 2 
//  15 << 2 = 111100
//                --
console.log(15 << 2); // 60
// number 2
// binary (10)
// shift by 5 
//  2 << 5 = 1000000
//             -----
console.log(2 << 5); // 64
// Right shift example
// number 15
// binary (1111)
// shift by 2 
//  15 >> 2 = 0011
//               
console.log(15 >> 2); // 3
// number 12
// binary (1100)
// shift by 1 
//  12 >> 1 = 0110
//             
console.log(12 >> 1); // 6

Relational Operators

Relational operator are work on two operands, and normally that are used to compare operands value. This operator are produce boolean result. And which are normally used in conditional statement to check expression.

Operator Name
> Greater than
< Less than
== Equal to equal to
>= Greater than equal to
<= Less than equal to
!= Not equal to
console.log(15 > 2);   // true
console.log(12 < 1);   // false
console.log(12 == 12); // true
console.log(12 >= 21); // false
console.log(12 <= 21); // true
console.log(12 != 12); // false 


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved