site logo

Learn All About Development


Category: (All)

Recent Posts:

Archive:

JavaScript Operators: Performing Calculations and Comparisons

Operators in JavaScript are symbols used to perform operations on values and variables. They are essential for calculations, comparisons, and decision-making in your code.


🔹 Arithmetic Operators

Used for basic math operations:

  1. + (Addition) → 5 + 3 = 8
  2. - (Subtraction) → 10 - 4 = 6
  3. * (Multiplication) → 2 * 3 = 6
  4. / (Division) → 12 / 4 = 3
  5. % (Modulus) → 10 % 3 = 1 (remainder)
  6. ** (Exponentiation) → 2 ** 3 = 8

🔹 Assignment Operators

Used to assign values to variables:

  1. = → Assigns a value (let x = 5;)
  2. += → Adds and assigns (x += 2;)
  3. -= → Subtracts and assigns (x -= 1;)
  4. *= → Multiplies and assigns (x *= 3;)

🔹 Comparison Operators

Used to compare values:

  1. == → Equal to (checks value only)
  2. === → Strict equal (checks value and type)
  3. != → Not equal
  4. !== → Strict not equal
  5. > → Greater than
  6. < → Less than
  7. >= → Greater than or equal to
  8. <= → Less than or equal to

🔹 Logical Operators

Used to combine conditions:

  1. && (AND) → True if both conditions are true
  2. || (OR) → True if at least one condition is true
  3. ! (NOT) → Reverses the condition


Example:

let age = 20;
if (age >= 18 && age < 30) {
console.log("You are a young adult.");
}

🔹 Best Practices
  1. Use === instead of == to avoid type conversion issues.
  2. Keep expressions simple for readability.
  3. Combine logical operators carefully to avoid confusion.


By mastering JavaScript operators, you’ll be able to perform calculations, make decisions, and control the flow of your programs effectively.




Comments (Write a comment)

Showing comments related to this blog.


Member's Sites: