Lesson 5

Decoding Arithmetic and Logical Operations in JavaScript

Introduction to Arithmetic and Logical Operations

Welcome! Today, we will dive into the universe of JavaScript arithmetic and logical operations. Just like in math, arithmetic operations help us conduct simple calculations. Logical operations, on the other hand, allow us to evaluate conditions and make decisions. They're like controllers in a video game, deciding your character's next move based on current conditions.

So, if you were in a candy store with only $10 to spend and each candy cost $2, what would you do? You'd likely perform a division operation to find out how many candies you could buy. But what if you wanted to buy ice cream ($4) and chips ($3) too with your $10? That's where logical operations come to the fore, helping you evaluate the right decision. Let's dive in!

Getting Started with Arithmetic Operations

Arithmetic operations are pretty straightforward in JavaScript, just as they are in math. We use + for addition, - for subtraction, * for multiplication, / for division, % for getting the remainder of the division, and finally, ** for power operator.

JavaScript
1// These are just like your typical calculator operations! 2 3// Addition 4let sum = 10 + 5; // Result: 15 5 6// Subtraction 7let difference = 10 - 5; // Result: 5 8 9// Multiplication 10let product = 10 * 5; // Result: 50 11 12// Division 13let quotient = 10 / 4; // Result: 2.5 14 15// Remainder 16let remainder = 14 % 4; // Result: 2 (10 = 4 * 3 + 2) 17 18// Power 19let powerResult = 10**3; // Result: 1000

As you can see above, the operators are placed between the numbers to conduct the calculations.

Integer division in JavaScript

You might wonder - what about integer division? If I have $10 and I would like to know how many $3 candies I can buy for it, how do I find out the answer? Well, all numbers in JavaScript are floating point, but there is a way to find the integer part of the number:

JavaScript
1const money = 10; 2const candyPrice = 3; 3 4const maxCandiesToBuy = Math.floor(money / candyPrice); // Prints: 3

So, in fact, we just apply Math.floor to retrieve the integer part (3) from the number 3.33333, rounding the number.

Discovering the Power of Logical Operations

While arithmetic operations revolve around numeric calculations, logical operations deal with conditions. They return a boolean value: true or false. JavaScript uses AND (&&), OR (||), and NOT (!) for logical operations.

JavaScript
1// Let's see how we can apply them: 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

Here, we used the actual boolean values — true and false — to illustrate the basic workings of logical operators. In real life, however, these true and false will likely be some expressions, e.g., (a > 3) && (a < 5).

Applying operations to variables

Applying operations to raw numbers is fun, but calculators can do the same! However, the most important feature of JavaScript is that you can operate with variables and apply all the above operations to variables as well. Here are some examples:

JavaScript
1const apples = 2; 2const applesPrice = 3.5; 3 4let totalPrice = apples * applesPrice; 5console.log("Total price:", totalPrice); 6 7const oranges = 3; 8const orangesPrice = 4; 9 10// The next line is a short form of totalPrice = totalPrice + oranges * orangesPrice; 11totalPrice += oranges * orangesPrice; 12console.log("Total price:", totalPrice);

As you can see, we first calculated the total price to purchase 2 apples, and then decided to buy 3 more oranges - it was as simple as updating the existing totalPrice variable, adding the price of 3 oranges to it. Note how we used the += operator here - it's basically just a short form of totalPrice = totalPrice + oranges * orangesPrice, which makes it very effective. You can use other operators like -=, *=, and /= the same way.

Lesson Summary and Practice

Kudos! You've become proficient in handling arithmetic and logical operations in JavaScript. Now, let's move into some practical exercises. Applying what you've learned in real scenarios will consolidate your knowledge and deepen your appreciation for JavaScript programming! Let's go!

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

Practice is how you turn knowledge into actual skills.