Let's now take a look at the several available operators you can leverage in TypeScript. Quite a few have already been implicitly introduced to you via code snippets from previous sections. Let's formally declare all the operators:
- Arithmetic operators: The most obvious class of operators are the arithmetic operators, which are used to perform arithmetic operations. The most obvious arithmetic operators are addition (
+
), subtraction (-
), multiplication (*
), division(/
), increment (++
), decrement (--
), and modulus (%
). The modulus operator returns the result of what remains after a number is divided by another as a whole. For example, take a look at the following code snippet:
const x: number = 9/4; //evaluates to 2.25 const y: number = 9%4; // evaluates to 1
As the usage of the remaining operators is natural, let's look at the next class of operators.
- Relational operators: These operators are used to perform comparisons in TypeScript. The common relational operators are equals (
==
), does not equal (!=
), greater than (>
), less than (<
), greater than or equals (>=
), and less than or equals (<=
).
Note
The equals (==
) operator exists in TypeScript in two flavors, strict mode and lenient mode. The ==
is the lenient mode, while the ===
or triple equals is the strict mode. With strict mode, the evaluation of the expression to true is less than lenient mode, because in lenient mode there is implicit type conversion whereas in strict mode there is no such conversion. For example, 16 == '16'
evaluates to true, whereas 16 === '16'
evaluates to false.
- Logical operators: These operators are used to perform manipulations on conditions. Most common logical operators are AND (
&&
), OR (||
), and Negation (!
). These operators can be used in various contexts, for example:
const condition: boolean = x > 0 && x < 10; // simple condition AND this.instanceVariable && this.instanceVariable.DoOperation(); /* AND used to check for non-null instance variable, and then perform the operation on that instance. */ // This could also be written as if (this.instanceVariable) { this.instanceVariable.DoOperation(); }
- Bitwise operators: Apart from the preceding mentioned operators, there are several bitwise operators, which perform advanced bit-level manipulations. Some common bitwise operators are as follows:
- Left shift (<<): Left shift shifts the bits in a number to the left, by the specified number of bits, for example, (
x << 2
) - Right shift (>>): Right shift shifts the bits in a number to the right, by the specified number of bits, for example, (
x >> 2
) - AND (&): The AND operator performs a bit-level AND on the bits of the two specified operands, for example, (
x & 4
, which isx & 0...0100
)
- OR (|): The OR operator performs a bit-level OR on the bits of the two specified operands, for example, (
x | 4
, which isx | 0...0100
) - XOR (^): XOR performs a bit-level XOR on the bits of the two specified operands, for example, (
x ^ y
) - Negation (~): Negation flips the bits of a number, for example, (
~x
)
Note
Some Important Bitwise Operator Hacks
1. x << 1 is the same as x * 2. 2. x >> 1 is the same as x / 2. 3. x & 0 will always equal 0. 4. x | 1 will always equal 1. 5. x ^ x will always equal 0. 6. ~0 is 11...11 (as many bits as the system supports).
For the sake of completeness, lets also explore the typeof
operator, instanceof
operator, and Compound AssignmentOperators with the help of the following code snippet:
// 1. compound assignments
let num: number = 40;
num *= 2; // num1 equals 80. this is same as num = num * 2
// 2. typeof
const str: String = 'test';
console.log(typeof num); // outputs number
console.log(typeof str === 'string'); // outputs true
// 3. instanceof
const arr: number[] = [1,2,3];
class ABC {};
const abc: ABC = new ABC();
console.log(arr instanceof Object); // outputs true
console.log(arr instanceof Array); // outputs true
console.log(abc instanceof Object); // outputs true
console.log(abc instanceof ABC); // outputs true
console.log(arr instanceof ABC); // outputs false
- Compound assignment operator: The first operator in the preceding code snippet is a compound multiplication assignment operator. This is the following two-step process:
- First is multiplication,
temp = num * 2
. - Second is assignment,
num = temp
.
- First is multiplication,
Thus, writing num *= 2
is equivalent to writing num = num *2
.
Similar to compound multiplication assignment, we can have compound addition, subtraction, and division assignments. Take a look at the following operators:
typeof
: The second operator in the preceding code snippet is thetypeof
assignment. As can be seen,typeof num
prints number andtypeof str
prints string.instanceof
: The third operator in the preceding code snippet is theinstanceof
operator. As can be seen in the preceding code snippet, this can be used to query a given object to find which class it is an instance of. This can be useful in determining the implementing class of an object passed to a method that accepts an interface type as a parameter.