TypeScript Operators

Introduction

In this chapter, we will explore the various operators available in TypeScript. Operators are special symbols or keywords that are used to perform operations on operands (variables and values).

Table of Contents

  1. Definition
  2. Types of Operators
    • Arithmetic Operators
    • Comparison Operators
    • Logical Operators
    • Assignment Operators
    • Bitwise Operators
    • Conditional (Ternary) Operator
  3. Examples and Output
  4. Conclusion

Definition

Operators in TypeScript are symbols or keywords that are used to perform operations on one or more operands. They are fundamental to performing calculations, making decisions, and manipulating data in your programs.

Types of Operators

Arithmetic Operators

Definition

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.

Syntax

let result = operand1 operator operand2;

Example

This example demonstrates the use of arithmetic operators.

let a: number = 10;
let b: number = 5;

let addition = a + b;        // Addition
let subtraction = a - b;     // Subtraction
let multiplication = a * b;  // Multiplication
let division = a / b;        // Division

console.log(addition);       // Output: 15
console.log(subtraction);    // Output: 5
console.log(multiplication); // Output: 50
console.log(division);       // Output: 2

Output

15
5
50
2

Comparison Operators

Definition

Comparison operators are used to compare two values and return a boolean result (true or false).

Syntax

let result = operand1 operator operand2;

Example

This example demonstrates the use of comparison operators.

let a: number = 10;
let b: number = 5;

let isEqual = a == b;       // Equal to
let isNotEqual = a != b;    // Not equal to
let isGreater = a > b;      // Greater than
let isLess = a < b;         // Less than
let isGreaterOrEqual = a >= b; // Greater than or equal to
let isLessOrEqual = a <= b; // Less than or equal to

console.log(isEqual);       // Output: false
console.log(isNotEqual);    // Output: true
console.log(isGreater);     // Output: true
console.log(isLess);        // Output: false
console.log(isGreaterOrEqual); // Output: true
console.log(isLessOrEqual); // Output: false

Output

false
true
true
false
true
false

Logical Operators

Definition

Logical operators are used to perform logical operations and return a boolean result. They are typically used with boolean values.

Syntax

let result = operand1 operator operand2;

Example

This example demonstrates the use of logical operators.

let a: boolean = true;
let b: boolean = false;

let andResult = a && b;  // Logical AND
let orResult = a || b;   // Logical OR
let notResult = !a;      // Logical NOT

console.log(andResult);  // Output: false
console.log(orResult);   // Output: true
console.log(notResult);  // Output: false

Output

false
true
false

Assignment Operators

Definition

Assignment operators are used to assign values to variables.

Syntax

variable operator value;

Example

This example demonstrates the use of assignment operators.

let a: number = 10;
a += 5;  // Equivalent to a = a + 5
a -= 3;  // Equivalent to a = a - 3
a *= 2;  // Equivalent to a = a * 2
a /= 4;  // Equivalent to a = a / 4

console.log(a);  // Output: 3.5

Output

3.5

Bitwise Operators

Definition

Bitwise operators are used to perform bit-level operations on binary values.

Syntax

let result = operand1 operator operand2;

Example

This example demonstrates the use of bitwise operators.

let a: number = 5;  // Binary: 0101
let b: number = 3;  // Binary: 0011

let andResult = a & b;  // Bitwise AND
let orResult = a | b;   // Bitwise OR
let xorResult = a ^ b;  // Bitwise XOR
let notResult = ~a;     // Bitwise NOT
let leftShift = a << 1; // Left shift
let rightShift = a >> 1; // Right shift

console.log(andResult);  // Output: 1
console.log(orResult);   // Output: 7
console.log(xorResult);  // Output: 6
console.log(notResult);  // Output: -6
console.log(leftShift);  // Output: 10
console.log(rightShift); // Output: 2

Output

1
7
6
-6
10
2

Conditional (Ternary) Operator

Definition

The conditional (ternary) operator is a shorthand for an if-else statement that assigns a value based on a condition.

Syntax

let result = condition ? value1 : value2;

Example

This example demonstrates the use of the conditional (ternary) operator.

let age: number = 18;
let canVote = (age >= 18) ? "Yes" : "No";

console.log(canVote);  // Output: Yes

Output

Yes

Complete Example

Let's put all the examples together in one file.

TypeScript Code (src/index.ts)

// Arithmetic Operators
let a: number = 10;
let b: number = 5;

let addition = a + b;
let subtraction = a - b;
let multiplication = a * b;
let division = a / b;

console.log(addition);       // Output: 15
console.log(subtraction);    // Output: 5
console.log(multiplication); // Output: 50
console.log(division);       // Output: 2

// Comparison Operators
let isEqual = a == b;
let isNotEqual = a != b;
let isGreater = a > b;
let isLess = a < b;
let isGreaterOrEqual = a >= b;
let isLessOrEqual = a <= b;

console.log(isEqual);       // Output: false
console.log(isNotEqual);    // Output: true
console.log(isGreater);     // Output: true
console.log(isLess);        // Output: false
console.log(isGreaterOrEqual); // Output: true
console.log(isLessOrEqual); // Output: false

// Logical Operators
let andResult = a > 0 && b > 0;
let orResult = a > 0 || b > 0;
let notResult = !(a > 0);

console.log(andResult);  // Output: true
console.log(orResult);   // Output: true
console.log(notResult);  // Output: false

// Assignment Operators
let c: number = 10;
c += 5;
c -= 3;
c *= 2;
c /= 4;

console.log(c);  // Output: 3.5

// Bitwise Operators
let bitwiseAnd = a & b;
let bitwiseOr = a | b;
let bitwiseXor = a ^ b;
let bitwiseNot = ~a;
let leftShift = a << 1;
let rightShift = a >> 1;

console.log(bitwiseAnd);  // Output: 1
console.log(bitwiseOr);   // Output: 15
console.log(bitwiseXor);  // Output: 15
console.log(bitwiseNot);  // Output: -11
console.log(leftShift);  // Output: 20
console.log(rightShift); // Output: 5

// Conditional (Ternary) Operator
let age: number = 18;
let canVote = (age >= 18) ? "Yes" : "No";

console.log(canVote);  // Output: Yes

Compiling to JavaScript

To compile the TypeScript code to JavaScript, run the TypeScript compiler:

tsc src/index.ts

Output in JavaScript (src/index.js)

// Arithmetic Operators
var a = 10;
var b = 5;

var addition = a + b;
var subtraction = a - b;
var multiplication = a * b;
var division = a / b;

console.log(addition);       // Output: 15
console.log(subtraction);    // Output: 5
console.log(multiplication); // Output: 50
console.log(division);       // Output: 2

// Comparison Operators
var isEqual = a == b;
var isNotEqual = a != b;
var isGreater = a > b;
var isLess = a < b;
var isGreaterOrEqual = a >= b;
var isLessOr

Equal = a <= b;

console.log(isEqual);       // Output: false
console.log(isNotEqual);    // Output: true
console.log(isGreater);     // Output: true
console.log(isLess);        // Output: false
console.log(isGreaterOrEqual); // Output: true
console.log(isLessOrEqual); // Output: false

// Logical Operators
var andResult = a > 0 && b > 0;
var orResult = a > 0 || b > 0;
var notResult = !(a > 0);

console.log(andResult);  // Output: true
console.log(orResult);   // Output: true
console.log(notResult);  // Output: false

// Assignment Operators
var c = 10;
c += 5;
c -= 3;
c *= 2;
c /= 4;

console.log(c);  // Output: 3.5

// Bitwise Operators
var bitwiseAnd = a & b;
var bitwiseOr = a | b;
var bitwiseXor = a ^ b;
var bitwiseNot = ~a;
var leftShift = a << 1;
var rightShift = a >> 1;

console.log(bitwiseAnd);  // Output: 1
console.log(bitwiseOr);   // Output: 15
console.log(bitwiseXor);  // Output: 15
console.log(bitwiseNot);  // Output: -11
console.log(leftShift);  // Output: 20
console.log(rightShift); // Output: 5

// Conditional (Ternary) Operator
var age = 18;
var canVote = (age >= 18) ? "Yes" : "No";

console.log(canVote);  // Output: Yes

Running the JavaScript

To see the output of the compiled JavaScript code, run the JavaScript file using Node.js:

node src/index.js

Conclusion

In this chapter, we covered the various operators available in TypeScript, including arithmetic, comparison, logical, assignment, bitwise, and conditional operators. We also provided examples with their outputs to illustrate how these operators work in TypeScript. Understanding these operators is essential for performing calculations, making decisions, and manipulating data in your TypeScript programs.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare