Aral Code

Decisions & Loops Lesson 11 of 30

JavaScript Comparisons and Logical Operators

Compare values with ===, !==, >, <, >= and <=, combine conditions with && (and), || (or) and ! (not), and understand truthy and falsy values.

Before a program can make decisions, it needs to ask questions: Is the grade at least 75? Is the password correct? Is the store open? Comparison operators ask these questions, and the answer is always a boolean: true or false.

Comparison operators

OperatorMeaningExampleResult
===Equal to5 === 5true
!==Not equal to5 !== 3true
>Greater than90 > 75true
<Less than60 < 75true
>=Greater than or equal75 >= 75true
<=Less than or equal74 <= 75true
Example Did the student pass?
<pre id="out"></pre>
<script>
  const grade = 78;
  document.getElementById("out").textContent =
    "grade >= 75: " + (grade >= 75) + "\n" +
    "grade === 100: " + (grade === 100) + "\n" +
    "grade !== 78: " + (grade !== 78);
</script>

=== versus ==

JavaScript has two "equal" operators:

  • === (strict equal) checks the value and the type. 5 === "5" is false.
  • == (loose equal) converts types first. 5 == "5" is true, and so is 0 == "", which is confusing.

Always use === and !==. They do what you expect.

Comparing strings

Strings compare letter by letter, and case matters:

"abc" === "abc"     // true
"Juan" === "juan"   // false (J vs j)
"apple" < "banana"  // true  (alphabetical)

Logical operators: and, or, not

OperatorNameTrue when…
&&ANDboth sides are true
||ORat least one side is true
!NOTflips true to false, false to true
Example Honor roll and discounts
<pre id="out"></pre>
<script>
  const average = 92;
  const lowestGrade = 86;
  const isHonor = average >= 90 && lowestGrade >= 85;

  const age = 67;
  const isPWD = false;
  const hasDiscount = age >= 60 || isPWD;

  const isOpen = false;
  document.getElementById("out").textContent =
    "With honors? " + isHonor + "\n" +
    "Senior/PWD discount? " + hasDiscount + "\n" +
    "Closed? " + !isOpen;
</script>

Checking a range

To check if a grade is between 80 and 89, you need two comparisons joined with AND:

grade >= 80 && grade <= 89

Writing 80 <= grade <= 89 looks like math, but JavaScript evaluates it wrongly (it compares true or false with 89). Always split it into two parts.

Truthy and falsy

In conditions, JavaScript treats some non-boolean values as false. These are the falsy values:

false, 0, "" (empty string), null, undefined and NaN.

Everything else is truthy, including "0", "false" and []. This lets you write short checks like "if the name box isn't empty":

const name = input.value.trim();
if (name) {
  // name is not an empty string
}

Default values with ||

|| returns the first truthy value, which is handy for defaults:

const nickname = typedNickname || "Guest";

If typedNickname is empty, nickname becomes "Guest". There's also ??, which only falls back for null or undefined (so a real 0 is kept): const score = savedScore ?? 0;.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What is 7 === "7"?
2. When is a && b true?
3. Which checks if x is from 1 to 10?
4. Which value is falsy?

By Sir Reyel · Last updated 2026-12-04