Skip to main content

Operators in TypeScript (Live Playground)

TypeScript supports a variety of operators that allow you to perform operations on variables and values.

In this tutorial, we will explore operators in TypeScript. Understanding how to use various operators will enable you to write clean, efficient, and effective TypeScript code.

Arithmetic Operators

Arithmetic operators perform mathematical operations:

TypeScript
let a = 10;
let b = 5;

let sum = a + b; // Addition
let difference = a - b; // Subtraction
let product = a * b; // Multiplication
let quotient = a / b; // Division
let remainder = a % b; // Modulus (remainder)
Live Playground, Try it Yourself

Comparison Operators

Comparison operators compare two values and return a boolean result:

TypeScript
let a = 10;
let b = 5;

let isEqual = a == b; // Equal
let isNotEqual = a != b; // Not equal
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
Live Playground, Try it Yourself

Logical Operators

Logical operators perform logical operations and return a boolean result:

TypeScript
let a = true;
let b = false;

let and = a && b; // Logical AND
let or = a || b; // Logical OR
let not = !a; // Logical NOT
Live Playground, Try it Yourself

Conclusion

In this tutorial, we have explored operators in TypeScript. Understanding how to use various operators effectively will enable you to write clean, efficient, and functional TypeScript code.