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
| Operator | Meaning | Example | Result |
|---|---|---|---|
=== | Equal to | 5 === 5 | true |
!== | Not equal to | 5 !== 3 | true |
> | Greater than | 90 > 75 | true |
< | Less than | 60 < 75 | true |
>= | Greater than or equal | 75 >= 75 | true |
<= | Less than or equal | 74 <= 75 | true |
<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 is0 == "", 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
| Operator | Name | True when… |
|---|---|---|
&& | AND | both sides are true |
|| | OR | at least one side is true |
! | NOT | flips true to false, false to true |
<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. 🙂
By Sir Reyel · Last updated 2026-12-04