### TypeScript - Arithmetic Operators ### Arithmetic Operators

An operator is a symbol that represents a specific action. An arithmetic operator can be used with one or more than values to produce a single value.

Operator NAME Description Example
- Subtraction It returns the difference of the values. x-y
* Multiplication It returns the product of the values. x*y
/ Division It performs the division operation, and returns the quotient. x/y
% Modulus It performs the division operation and returns the remainder. x%y
++ Increment It is used to increments the value of the variable by one. x++
-- Decrement It is used to decrements the value of the variable by one. x--
```

let x : number=10;
let y : number=4;
let result :number=0;

result = x+y;
console.log(`Addition of x and y : \${result}` );  // 14

// Subtraction
result = x-y;
console.log(`Subtraction of x and y : \${result}` );  // 6

result = x*y;
console.log(`Multiplication of x and y : \${result}` );  // 40

// Division
result = x/y;
console.log(`Division of x and y : \${result}` );  // 2.5

// Modulus (Remainder)
result = x%y;
console.log(`Modulus (Remainder) of x and y : \${result}` );  // 2

// Increment
x++;
console.log(`Increment of x: \${x}` );  // 11

// Increment
y--;
console.log(`Decrement of y : \${y}` );  // 3

// Addition of x and y : 14
// Subtraction of x and y : 6
// Multiplication of x and y : 40
// Division of x and y : 2.5
// Modulus (Remainder) of x and y : 2
// Increment of x: 11
// Decrement of y : 3

```

### Comparison Operators

The comparison operators are used to compares the two operands. These operators return a Boolean value true or false. The important comparison operators are given below.

 Operator NAME Description Example == Is equal to It checks whether the values of the two operands are equal or not. x==y === Identical (equal and of same type) It checks whether the type and values of the two operands are equal or not. x===y != Not equal to It checks whether the values of the two operands are equal or not. x !=y !== Not Identical It checks whether the type and values of the two operands are equal or not. x !== y > Greater than It checks whether the value of the left operands is greater than the value of the right operand or not. x >y >= Greater than or equal to It checks whether the value of the left operands is greater than or equal to the value of the right operand or not. x >= y < Less than It checks whether the value of the left operands is less than the value of the right operand or not. x
```

let x:number= 5;
let y:number=10;
let z:any='5';

// Is equal to (==)
console.log(x ==y );        // false
console.log(x== 5);         // true
console.log(x == z);        // true

// Identical(===)
console.log(x ===y );        // false
console.log(x=== 5);         // true
console.log(x === z);        // false

// Not equal to (!=)
console.log(x !=y );        // true
console.log(x != 5);        // false
console.log(x != z);        // false

// Not identical (!==)
console.log(x !== y );        // true
console.log(x !== 5);        // false
console.log(x !== z);        // true

// Greater than(>)
console.log(x > y );        // false
console.log(x > 2);        // true
console.log(x > z);        // false

// Greater than or equal to (>=)
console.log(x >= y );        // false
console.log(x >= 5);        // true
console.log(x >= z);        // true

// Less than (<) console.log(x < y );        // true
console.log(x < 2);        // false
console.log(x < z);        // false

// 	Less than or equal to(<=)

console.log(x <= y );        // true
console.log(x <= 2);        // false
console.log(x <= z);        // true

```

### Logical Operators

Logical operators are typically used with  `Boolean`  (logical) values. When they are, they return a Boolean value. However, the `&&` and `||` operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they will return a non-Boolean value.

 Operator NAME Description Example && Logical AND It returns true if both the operands(expression) are true, otherwise returns false. x&&y || Logical OR It returns true if any of the operands(expression) are true, otherwise returns false. x||y ! Logical NOT It returns the inverse result of an operand(expression). !x

If a value can be converted to `true`, the value is so-called truthy. If a value can be converted to `false`, the value is so-called falsy.

Examples of expressions that can be converted to false are:

• `null`;
• `NaN`;
• `0`;
• empty string (`""` or `''` or ````);
• `undefined`.
```

let x =true;
let y =false;
let z= true;

console.log(x && y);             // false
console.log(x && true);         // true
console.log(x && 2);            // 2
console.log(y && '2');          // false

console.log(x || y);             // false
console.log(x || true);         // true
console.log(x || 2);            // 2
console.log(y || '2');          // false

console.log( !x);             // false
console.log( !y);            // true
console.log(!true );         // false
console.log(!false );        // true
console.log(!null );         //true

```

### Bitwise operators

Bitwise operators treat their operands as a sequence of 32 bits (zeroes and ones), rather than as decimal, hexadecimal, or octal `numbers`. For example, the decimal number nine has a binary representation of 1001. Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values.

Operator NAME Description Example
& Bitwise AND Returns a `1` in each bit position for which the corresponding bits of both operands are `1`s. a & b
| Bitwise OR Returns a `1` in each bit position for which the corresponding bits of either or both operands are `1`s. a | b
Bitwise XOR Returns a `1` in each bit position for which the corresponding bits of either but not both operands are `1`s. a ^ b
~  Bitwise NOT  Inverts the bits of its operand.  ~ a
<<   Bitwise Left Shift  Shifts `a` in binary representation `b` (< 32) bits to the left, shifting in `0`s from the right.  a << b
>>  Bitwise Right Shift  Shifts `a` in binary representation `b` (< 32) bits to the right, discarding bits shifted off.  a >> b
>>>  Bitwise Right Shift with Zero  Shifts `a` in binary representation `b` (< 32) bits to the right, discarding bits shifted off, and shifting in `0`s from the left.  `a >>> b`

### Bit-wise AND ( & ) :

It is a binary operator i.e. accepts two operands. Bit-wise AND (&) returns 1 if both the bits are set ( i.e 1) and 0 in any other case.

A B OUTPUT ( A & B )
0 0 0
0 1 0
1 0 0
1 1 1

### Bit-Wise OR ( | ) :

Its is a binary operator i.e. accepts two operands. Bit-wise OR ( | ) returns 1 if any of the operand is set (i.e. 1) and 0 in any other case.

A B OUTPUT ( A | B )
0 0 0
0 1 1
1 0 1
1 1 1

### Bit-Wise XOR ( ^ ) :

Its is a binary operator i.e. accepts two operands. Bit-wise XOR ( ^ ) returns 1 if both the operands are different and 0 in any other case.

A B OUTPUT ( A ^ B )
0 0 0
0 1 1
1 0 1
1 1 0

### Bit-Wise NOT ( ~ ) :

Its is a unary operator i.e. accepts single operands. Bit-wise NOT ( ~ ) flips the bits i.e 0 becomes 1 and 1 becomes 0.

A OUTPUT ( ~A )
0 1
1 0
```

let a =8;
let b=10;

console.log(a & b);         // 8
console.log(a | b);         //10
console.log(a ^ b);         //2
console.log(~a);            //9
console.log(a >> b);        //0
console.log(a << b);        //8192
console.log(a >>> b);       //0

console.log(5 & 13); // 0101 & 1101 = 0101
// expected output: 5;

console.log(parseInt('0101', 2) & parseInt('1101', 2));
// expected output: 5;

console.log(5 & 13 & 3); // 0101 & 1101 & 0011 = 0001
// expected output: 1;

console.log(5 | 13); // 0101 | 1101 = 1101
// expected output: 13

```

### Assignment Operators

Assignment operators are used to assign values to variables. This type of statement consists of a variable name, an assignment operator, and an expression. When appropriate, you can declare a variable and assign a value to it in a single statement. In assignment expressions, the right-hand expression is contextually typed by the type of the left-hand expression. The first assignment operator in the table assigns the value (unmodified) of the expression on the right of the equal sign to the variable on the left. The other five operators are called compound assignment operators that perform the specified operation on the right-side value before assigning a value to the left side.

Operator NAME Description Example
= Assign It assigns values from right side to left side operand. x = y
+= Add and assign It adds the left operand with the right operand and assigns the result to the left side operand. x += y
-= Subtract and assign It subtracts the right operand from the left operand and assigns the result to the left side operand. x -= y
*= Multiply and assign It multiplies the left operand with the right operand and assigns the result to the left side operand. x *= y
/= Divide and assign It divides the left operand with the right operand and assigns the result to the left side operand x /= y
%= Modulus and assign It divides the left operand with the right operand and assigns the result to the left side operand. x %= y