TypeScript Operators: Learn Bitwise, Assignment, and Comma Operators with Examples

TypeScript Bitwise Operators

Bitwise operators are used for evaluating bit-based operations. Sometimes, when we need to manipulate bits of integer values, we utilize bitwise operators.

These operators, similar to many programming languages, work with 32 bits of an integer and operate on binary operands.

Syntax:

Operand1 Operator Operand2

Where Operand1 and Operand2 are JavaScript expressions, and Operators are bitwise operators.

Let’s assume the values of A and B are 5 and 10, and observe the results:

OperatorTitleDescriptionExample
&Bitwise ANDEvaluates And operation on bits Integers parametersA & B is 0
\Bitwise OREvaluates Or operation on bits Integers parametersA \ B is 15
^Bitwise XOREvaluates exclusive operation on bits of Integers parametersA ^ B is 15
~Bitwise NOTNot operator on bits of parameter~A is -6
<<Left ShiftsFirst shifts left bits of the first operand by second operand times and the remaining bits will be filled with zeroA<<
>>Right ShiftsFirst shifts right bits of the first operand by second operand times and the remaining bits will be filled with zeroA>>B is 0
>>>Right Shifts with zeroEvaluation is right shift + left Bits shifted replaced with zeroA>>>B is 0

Following is a Bitwise operators Example.

var A = 5;
var B = 10;
console.log(A & B);   // returns 0
console.log(A | B);   // returns 15
console.log(~A);      // returns -6
console.log(A ^ B);   // returns 15
console.log(A << 1);  // returns 10
console.log(A >> 1);  // returns 2

TypeScript Assignment Operators

Assignment operators perform operations and assign values from left to right.

Syntax:

Operand1 Assignment-operator Operand2

The following table explains it with examples.

| Operator | Title | Description| Example| | :------- | :--------------------------- | ------------------ | ---------------- | ------------------ | | = | Simple assign | Evaluates, And operation on bits Integers parameters | a=2 and value of a is 2 | | += | Add And assign | Add right operand to left and assign the result to left operand | a+=2 and value of a is equal to a=a+2 | | -= | Subtract and assign | Subtract right operand to left and assign the result to left operand | a-=2 and value of a is equal to a=a+2 | | *= | Multiply And assign | Multiply right operand with left and assign the result to left operand | a*a=2 is equal to a=a*2 | | /= | Divide and assign | Divide right operand with left and assign the result to left operand | c/=2 is equal to c=c/2 | | %= | Modulus And Assign | Divide right operand with left and assign the result of the remainder to left operand | c%=2 is equal to c=c%2 | | <<= | Left Shifts And assign | Perform left shift and result is assigned to left operand | c<<=2 is equal to c=c<<2 | | >>= | Right Shifts And assign | Perform Right shift and result is assigned to left operand | c>>=2 is equal to c=c>>2 | | &= | Bitwise And Assign | Perform Bitwise And and the result is assigned to left operand | c&=2 is equal to c=c&2 | | ^= | Bitwise Exclusive And Assign | Perform Bitwise Exclusive OR and result is assigned to left operand | c^=2 is equal to c=c^2 | | \|= | Bitwise Or And Assign | Perform Bitwise inclusive OR and the result is assigned to left operand | c | =2is equal toc=c | 2 |

TypeScript Comma Operator

The comma operator (,) is used to perform multiple expressions and return the value of the last expression. Expressions are evaluated from left to right.

Syntax :-

Operand / Expression, Operand2;

Here is an example Example

var variable = 10;
variable = (--variable, variable);
console.log(variable); // outputs 9

Conclusion

In conclusion, you’ve learned about TypeScript operators with examples, including bitwise operators, assignment operators, and the comma operator.