Mastering TypeScript Arithmetic and Logical Operations

Introduction to Arithmetic and Logical Operations

Getting Started with Arithmetic Operations

Arithmetic operations in TypeScript are simple and bear a strong resemblance to those in basic mathematics. We use + for addition, - for subtraction, * for multiplication, / for division, % for determining the remainder of a division, and ** for exponentiation.

// Consider these as standard calculator operations!

// Addition
let sum = 20 + 5; // Result: 25

// Subtraction
let difference = 20 - 5; // Result: 15

// Multiplication
let product = 20 * 5; // Result: 100

// Division
let quotient = 20 / 4; // Result: 5

// Remainder
let remainder = 18 % 4; // Result: 2 (18 = 4 * 4 + 2)

// Power
let powerResult = 20**2; // Result: 400

As showcased in the example above, operators are inserted between two numbers to perform the calculations.

Integer Division in TypeScript

Exploring the Power of Logical Operations

While arithmetic operations focus on numeric computations, logical operations deal with conditions. They yield a boolean value: true or false. TypeScript uses AND (&&), OR (||), and NOT (!) to perform logical operations.

// Let's understand their usage:

console.log(true && true);      // 'true' AND 'true' = true 
console.log(true && false);     // 'true' AND 'false' = false
console.log(true || false);     // 'true' OR 'false' = true
console.log(false || false);    // 'false' OR 'false' = false
console.log(!true);             // NOT 'true' = false
console.log(!false);            // NOT 'false' = true

In this instance, we used the actual boolean values — true and false — to illustrate the fundamental functioning of logical operators. However, in real coding scenarios, these true and false values would likely be expressions, such as (b > 5) && (b < 8).

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal