Lesson 13 +15 XP

Comparison and Logical Operators

Comparison and Logical Operators

Comparison operators compare values and return true or false.

Comparison operators

OperatorMeaningExample (x = 5)
==equal to (loose)x == 5 true
===equal value and type (strict)x === 5 true
!=not equal (loose)x != 8 true
!==not equal value or type (strict)x !== 8 true
>greater thanx > 3 true
<less thanx < 3 false
>=greater or equalx >= 5 true
<=less or equalx <= 4 false

Loose vs strict

  • == compares values only, so 5 == "5" is true.
  • === compares value and type, so 5 === "5" is false.
  • Always prefer === to avoid surprises.

Logical operators

OperatorMeaningExample
&&AND, true if both are truea && b
``OR, true if either is true`ab`
!NOT, flips the result!a

Examples

let age = 20;
age >= 18 && age < 65; // true (adult working age)
age < 18 || age > 65;  // false
!(age < 18);           // true

TL;DR

  • Comparison operators return true or false.
  • Use === and !== for safe comparisons.
  • && AND, || OR, ! NOT.