DEV Community

Cover image for Day 3: Mastering Operators and Expressions in JavaScript
Dipak Ahirav
Dipak Ahirav

Posted on • Updated on

Day 3: Mastering Operators and Expressions in JavaScript

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 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.

let sum = 5 + 3;
console.log(sum); // Output: 8
Enter fullscreen mode Exit fullscreen mode

2. Subtraction (-)
Subtracts the second number from the first.

let difference = 9 - 4;
console.log(difference); // Output: 5
Enter fullscreen mode Exit fullscreen mode

3. Multiplication (*)
Multiplies two numbers.

let product = 7 * 2;
console.log(product); // Output: 14
Enter fullscreen mode Exit fullscreen mode

4. Division (/)
Divides the first number by the second.

let quotient = 10 / 2;
console.log(quotient); // Output: 5
Enter fullscreen mode Exit fullscreen mode

5. Modulus (%)
Returns the remainder of the division.

let remainder = 10 % 3;
console.log(remainder); // Output: 1
Enter fullscreen mode Exit fullscreen mode

6. Increment (++)
Increases a number by one.

let count = 5;
count++;
console.log(count); // Output: 6
Enter fullscreen mode Exit fullscreen mode

7. Decrement (--)
Decreases a number by one.

let count = 5;
count--;
console.log(count); // Output: 4
Enter fullscreen mode Exit fullscreen mode

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).

let isEqual = (5 == '5');
console.log(isEqual); // Output: true
Enter fullscreen mode Exit fullscreen mode

2. Strict Equal (===)
Checks if two values are equal and of the same type.

let isStrictEqual = (5 === '5');
console.log(isStrictEqual); // Output: false
Enter fullscreen mode Exit fullscreen mode

3. Not Equal (!=)
Checks if two values are not equal (type conversion may occur).

let isNotEqual = (5 != '5');
console.log(isNotEqual); // Output: false
Enter fullscreen mode Exit fullscreen mode

4. Strict Not Equal (!==)
Checks if two values are not equal or not of the same type.

let isStrictNotEqual = (5 !== '5');
console.log(isStrictNotEqual); // Output: true
Enter fullscreen mode Exit fullscreen mode

5. Greater Than (>)
Checks if the left value is greater than the right value.

let isGreaterThan = (6 > 3);
console.log(isGreaterThan); // Output: true
Enter fullscreen mode Exit fullscreen mode

6. Less Than (<)
Checks if the left value is less than the right value.

let isLessThan = (6 < 3);
console.log(isLessThan); // Output: false
Enter fullscreen mode Exit fullscreen mode

7. Greater Than or Equal (>=)
Checks if the left value is greater than or equal to the right value.

let isGreaterThanOrEqual = (6 >= 6);
console.log(isGreaterThanOrEqual); // Output: true
Enter fullscreen mode Exit fullscreen mode

8. Less Than or Equal (<=)
Checks if the left value is less than or equal to the right value.

let isLessThanOrEqual = (6 <= 6);
console.log(isLessThanOrEqual); // Output: true
Enter fullscreen mode Exit fullscreen mode

Logical Operators

Logical operators are used to combine multiple conditions.

1. Logical AND (&&)
Returns true if both operands are true.

let andResult = (5 > 3 && 8 > 6);
console.log(andResult); // Output: true
Enter fullscreen mode Exit fullscreen mode

2. Logical OR (||)
Returns true if at least one operand is true.

let orResult = (5 > 3 || 8 < 6);
console.log(orResult); // Output: true
Enter fullscreen mode Exit fullscreen mode

3. Logical NOT (!)
Inverts the truthiness of the operand.

let notResult = !(5 > 3);
console.log(notResult); // Output: false
Enter fullscreen mode Exit fullscreen mode

Expressions

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

Example:

let a = 10;
let b = 5;
let c = a + b * 2;
console.log(c); // Output: 20
Enter fullscreen mode Exit fullscreen mode

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:

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);
Enter fullscreen mode Exit fullscreen mode

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

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:

Top comments (0)