Lesson 5

Mastering TypeScript Arithmetic and Logical Operations

Introduction to Arithmetic and Logical Operations

Welcome! Today, we will traverse the galaxy of TypeScript arithmetic and logical operations. Arithmetic operations, similar to basic mathematics, help us conduct simple calculations. Logical operations, on the contrary, assist us in evaluating conditions and making decisions. They act as game controllers, steering your character's next action based on the current circumstances.

Suppose you were in a toy store with only $20 at hand, and each toy was priced at $5. Most likely, you would perform a division operation to determine how many toys you could afford. But what if you wanted to buy a board game ($7) and a puzzle ($6) with your $20? Logical operations would then come into play, helping you make the right decision. Let's explore!

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.

TypeScript
1// Consider these as standard calculator operations! 2 3// Addition 4let sum = 20 + 5; // Result: 25 5 6// Subtraction 7let difference = 20 - 5; // Result: 15 8 9// Multiplication 10let product = 20 * 5; // Result: 100 11 12// Division 13let quotient = 20 / 4; // Result: 5 14 15// Remainder 16let remainder = 18 % 4; // Result: 2 (18 = 4 * 4 + 2) 17 18// Power 19let 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

A common question is - how to perform integer division? If I have $21 and want to know how many $4 toys I can purchase, how can I calculate this? All numbers in TypeScript are floating point, but there is a way to identify the integer part of a number:

TypeScript
1const budget = 21; 2const toyPrice = 4; 3 4const maxToysToBuy = Math.floor(budget / toyPrice); // Prints: 5

So, in essence, we use Math.floor to obtain the integer part (5) from the number 5.25, rounding it down.

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.

TypeScript
1// Let's understand their usage: 2 3console.log(true && true); // 'true' AND 'true' = true 4console.log(true && false); // 'true' AND 'false' = false 5console.log(true || false); // 'true' OR 'false' = true 6console.log(false || false); // 'false' OR 'false' = false 7console.log(!true); // NOT 'true' = false 8console.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).

Applying Operations to Variables

Performing operations on raw numbers is useful, but calculators offer similar functionality! However, TypeScript's most important feature is the ability to manipulate variables and apply all the aforementioned operations to these variables as well. Let's observe some examples:

TypeScript
1const dolls = 3; 2const dollsPrice = 6.5; 3 4let totalAmount = dolls * dollsPrice; 5console.log("Total Amount:", totalAmount); 6 7const cars = 2; 8const carsPrice = 7; 9 10// The following line is a short form of totalAmount = totalAmount + cars * carsPrice; 11totalAmount += cars * carsPrice; 12console.log("Total Amount:", totalAmount);

Output:

1Total Amount: 19.5 2Total Amount: 33.5

As demonstrated above, we first calculated the total amount to buy 3 dolls, and then decided to buy 2 more cars. This process was as simple as updating the existing totalAmount variable by adding the price of 2 cars to it. Note that we used the += operator - it's essentially a condensed form of totalAmount = totalAmount + cars * carsPrice, which makes it very efficient. Similar operators like -=, *=, and /= can be used in the same manner.

Lesson Recap and Application

Congratulations! You have now become proficient in managing arithmetic and logical operations in TypeScript. Now, let's apply what you have learned through some practical exercises. Implementing your knowledge in realistic situations will reinforce your understanding and foster a deeper appreciation for TypeScript programming! Let's dive in!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.