Day 3: Mastering Operators and Expressions in JavaScript
Tags: javascript, beginners, html, css
Published: 2024-05-31 14:35:04

#### Introduction
Welcome to Day 3 of your JavaScript journey! Yesterday, we explored variables and data types. Today, we will dive into operators and expressions, which are fundamental for performing calculations and making decisions in your code.

please subscribe to my [YouTube channel](https://www.youtube.com/@DevDivewithDipak?sub_confirmation=1) to support my channel and get more web development tutorials.

#### Arithmetic Operators

Arithmetic operators allow you to perform mathematical operations on numbers.

**1. Addition (+)**
Adds two numbers.
```javascript
let sum = 5 + 3;
console.log(sum); // Output: 8
```

**2. Subtraction (-)**
Subtracts the second number from the first.
```javascript
let difference = 9 - 4;
console.log(difference); // Output: 5
```

**3. Multiplication (*)**
Multiplies two numbers.
```javascript
let product = 7 * 2;
console.log(product); // Output: 14
```

**4. Division (/)**
Divides the first number by the second.
```javascript
let quotient = 10 / 2;
console.log(quotient); // Output: 5
```

**5. Modulus (%)**
Returns the remainder of the division.
```javascript
let remainder = 10 % 3;
console.log(remainder); // Output: 1
```

**6. Increment (++)**
Increases a number by one.
```javascript
let count = 5;
count++;
console.log(count); // Output: 6
```

**7. Decrement (--)**
Decreases a number by one.
```javascript
let count = 5;
count--;
console.log(count); // Output: 4
```

#### Comparison Operators

Comparison operators compare two values and return a boolean (true or false).

**1. Equal (==)**
Checks if two values are equal (type conversion may occur).
```javascript
let isEqual = (5 == '5');
console.log(isEqual); // Output: true
```

**2. Strict Equal (===)**
Checks if two values are equal and of the same type.
```javascript
let isStrictEqual = (5 === '5');
console.log(isStrictEqual); // Output: false
```

**3. Not Equal (!=)**
Checks if two values are not equal (type conversion may occur).
```javascript
let isNotEqual = (5 != '5');
console.log(isNotEqual); // Output: false
```

**4. Strict Not Equal (!==)**
Checks if two values are not equal or not of the same type.
```javascript
let isStrictNotEqual = (5 !== '5');
console.log(isStrictNotEqual); // Output: true
```

**5. Greater Than (>)**
Checks if the left value is greater than the right value.
```javascript
let isGreaterThan = (6 > 3);
console.log(isGreaterThan); // Output: true
```

**6. Less Than (<)**
Checks if the left value is less than the right value.
```javascript
let isLessThan = (6 < 3);
console.log(isLessThan); // Output: false
```

**7. Greater Than or Equal (>=)**
Checks if the left value is greater than or equal to the right value.
```javascript
let isGreaterThanOrEqual = (6 >= 6);
console.log(isGreaterThanOrEqual); // Output: true
```

**8. Less Than or Equal (<=)**
Checks if the left value is less than or equal to the right value.
```javascript
let isLessThanOrEqual = (6 <= 6);
console.log(isLessThanOrEqual); // Output: true
```

#### Logical Operators

Logical operators are used to combine multiple conditions.

**1. Logical AND (&&)**
Returns true if both operands are true.
```javascript
let andResult = (5 > 3 && 8 > 6);
console.log(andResult); // Output: true
```

**2. Logical OR (||)**
Returns true if at least one operand is true.
```javascript
let orResult = (5 > 3 || 8 < 6);
console.log(orResult); // Output: true
```

**3. Logical NOT (!)**
Inverts the truthiness of the operand.
```javascript
let notResult = !(5 > 3);
console.log(notResult); // Output: false
```

#### Expressions

Expressions are combinations of variables, operators, and values that yield a result.

**Example:**
```javascript
let a = 10;
let b = 5;
let c = a + b * 2;
console.log(c); // Output: 20
```

#### Practice Activities

**1. Practice Code:**
   - Write expressions using each arithmetic, comparison, and logical operator.
   - Combine multiple operators in complex expressions.

**2. Mini Project:**
   - Create a simple calculator script that performs basic arithmetic operations based on user input.

**Example:**
```javascript
let num1 = parseFloat(prompt("Enter the first number:"));
let num2 = parseFloat(prompt("Enter the second number:"));
let operation = prompt("Enter the operation (+, -, *, /, %):");

let result;
if (operation === "+") {
  result = num1 + num2;
} else if (operation === "-") {
  result = num1 - num2;
} else if (operation === "*") {
  result = num1 * num2;
} else if (operation === "/") {
  result = num1 / num2;
} else if (operation === "%") {
  result = num1 % num2;
} else {
  result = "Invalid operation";
}

console.log("Result:", result);
```

#### Summary

Today, we explored operators and expressions in JavaScript. We learned how to use arithmetic, comparison, and logical operators to build expressions and perform calculations. Understanding these operators is crucial for making decisions and manipulating data in your code.

Stay tuned for Day 4, where we'll dive into control structures like conditionals and loops!

#### Resources

- [Expressions and Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators)
- [JavaScript Basics](https://developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/JavaScript_basics)

Happy coding! If you have any questions or need further clarification, feel free to leave a comment below. Let's continue learning and growing together!

*Follow me for more tutorials and tips on web development. Feel free to leave comments or questions below!*

#### Follow and Subscribe:
- **Website**: [Dipak Ahirav] (https://www.dipakahirav.com)
- **Email**: dipaksahirav@gmail.com
- **Instagram**: [devdivewithdipak](https://www.instagram.com/devdivewithdipak)
- **YouTube**: [devDive with Dipak](https://www.youtube.com/@DevDivewithDipak?sub_confirmation=1)
- **LinkedIn**: [Dipak Ahirav](https://www.linkedin.com/in/dipak-ahirav-606bba128)