DeveloperBreeze

Comparison and Logical Operators

Section 2.3: Operators

Tutorial 2.3.2: Comparison and Logical Operators


Comparison and Logical Operators in JavaScript

Comparison and logical operators allow you to compare values and perform logical operations. They are fundamental for decision-making in JavaScript, such as in if statements and loops.


1. Comparison Operators

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

OperatorSymbolDescriptionExampleResult
Equal to==Checks if values are equal5 == '5'true
Strict Equal to===Checks if values and types are equal5 === '5'false
Not Equal to!=Checks if values are not equal5 != '5'false
Strict Not Equal to!==Checks if values and types are not equal5 !== '5'true
Greater than>Checks if left is greater than right10 > 5true
Greater than or Equal to>=Checks if left is greater than or equal to right10 >= 10true
Less than<Checks if left is less than right3 < 8true
Less than or Equal to<=Checks if left is less than or equal to right3 <= 3true

Examples of Comparison Operators

console.log(5 == "5"); // true (type coercion)
console.log(5 === "5"); // false (no type coercion)
console.log(10 > 7); // true
console.log(4 <= 4); // true

2. Logical Operators

Logical operators combine multiple conditions or values.

OperatorSymbolDescriptionExampleResult
AND&&Returns true if both conditions are truetrue && falsefalse
OR||Returns true if at least one condition is truetrue || falsetrue
NOT!Reverses the boolean value!truefalse

Examples of Logical Operators

// AND operator
console.log(true && true); // true
console.log(true && false); // false

// OR operator
console.log(false || true); // true
console.log(false || false); // false

// NOT operator
console.log(!true); // false
console.log(!false); // true

Combining Comparison and Logical Operators

Use comparison and logical operators together for complex conditions.

Example:

let age = 20;
let hasID = true;

if (age >= 18 && hasID) {
  console.log("Access granted.");
} else {
  console.log("Access denied.");
}
// Output: "Access granted."

Common Errors

  1. Confusing == with ===:
  • == performs type coercion.
  • === ensures both value and type match.
  1. Using && or || incorrectly:
  • Logical operators evaluate left to right; ensure your conditions are correct.

Exercise

  1. Check if a number is greater than 10 and even.
  2. Write a condition to determine if a user can log in based on the following:
  • The user must be logged in (isLoggedIn is true).
  • The user's account must not be suspended (isSuspended is false).

Example:

let number = 12;
if (number > 10 && number % 2 === 0) {
  console.log("The number is greater than 10 and even.");
}

let isLoggedIn = true;
let isSuspended = false;

if (isLoggedIn && !isSuspended) {
  console.log("User can log in.");
} else {
  console.log("Access denied.");
}

Related Posts

More content you might like

Tutorial
javascript

Arithmetic Operators

     let x = "5";
     let num = +x; // 5 (number)
  • Converts the operand to a number and negates it.
  • Example:

Dec 11, 2024
Read More
Tutorial
javascript

Non-Primitive Data Types (Objects, Arrays, and Functions)

  • Adding Elements:
  fruits.push("orange");
  console.log(fruits); // ["apple", "banana", "cherry", "orange"]

Dec 11, 2024
Read More
Tutorial
javascript

Primitive Data Types

  let isLoggedIn = true;
  let hasAccess = false;
  console.log(isLoggedIn && hasAccess); // false

A variable that has been declared but not assigned a value is undefined.

Dec 11, 2024
Read More
Tutorial
javascript

Variables and Constants

     const PI = 3.14;
     console.log(PI); // 3.14
  • Valid: name, _age, $price
  • Invalid: 1name, @value

Dec 10, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!