Skip to main content

Archive

Show more

Operators in TypeScript

Operators in TypeScript

TypeScript includes a variety of operators to perform operations on variables and values. These operators can be categorized into several types: Arithmetic, Comparison, Logical, and more. Below are detailed tables for each type of operator, followed by examples and explanations.


Arithmetic Operators

Operator Description
+ Addition (also used for string concatenation)
- Subtraction
* Multiplication
/ Division
% Modulus (remainder)
++ Increment
-- Decrement

Arithmetic operators are used to perform basic mathematical operations. For example, addition can combine numbers or concatenate strings.


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

console.log(a + b); // Output: 15
console.log(a * b); // Output: 50
console.log(a % 3); // Output: 2

Comparison Operators

Operator Description
== Equality (loose)
=== Strict Equality
!= Inequality (loose)
!== Strict Inequality
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Comparison operators are used to compare values and return a boolean result based on the comparison. They are essential for making decisions in your code.


let x: number = 10;
let y: number = 20;

console.log(x === y); // Output: false
console.log(x < y); // Output: true

Logical Operators

Operator Description
&& Logical AND
|| Logical OR
! Logical NOT

Logical operators are used to perform boolean operations and combine multiple conditions.


let isTrue: boolean = true;
let isFalse: boolean = false;

console.log(isTrue && isFalse); // Output: false
console.log(isTrue || isFalse); // Output: true
console.log(!isTrue); // Output: false

Conditional Operators

Operator Description
? Conditional (ternary) operator

The conditional (ternary) operator is a shorthand for an if-else statement and is used to evaluate expressions based on a condition.


let age: number = 18;
let isAdult: string = (age >= 18) ? 'Yes' : 'No';

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

Other Operators

Operator Description
instanceof Checks if an object is an instance of a class
in Checks if a property exists in an object
, Comma operator (evaluates multiple expressions)

Other operators include `instanceof` to check object types, `in` to check for properties, and the comma operator to evaluate multiple expressions in a single statement.


class Person {
  name: string;

  constructor(name: string) {
    this.name = name;
  }
}

let john = new Person('John');

console.log(john instanceof Person); // Output: true

let obj = { name: 'Alice', age: 25 };

console.log('name' in obj); // Output: true
console.log('address' in obj); // Output: false

Conclusion

TypeScript’s operators are essential tools for performing various operations on variables and values. Understanding the different types of operators—Arithmetic, Comparison, Logical, Conditional, and others—can significantly enhance your ability to write effective and efficient TypeScript code. By leveraging these operators, you can perform mathematical calculations, make comparisons, combine boolean conditions, and handle different scenarios in your application logic.

Comments