Lesson 33 of 296
Booleans and comparison signs
What you will learn
- What booleans are for
- The comparison operators
- How comparisons produce
trueorfalse - How strings are compared
- How to store the result of a comparison
On this page
Booleans
A boolean is a value that is either true or false. Booleans are how programs make decisions: show the page or not, allow the purchase or not.
const isAdult = true;
const isSoldOut = false;Comparisons give booleans
A comparison asks a yes/no question. The answer is always a boolean.
| Operator | Question | Example | Result |
|---|---|---|---|
> | Is the left bigger than the right? | 8 > 5 | true |
< | Is the left smaller than the right? | 8 < 5 | false |
>= | Bigger or equal? | 5 >= 5 | true |
<= | Smaller or equal? | 4 <= 5 | true |
=== | Exactly equal (value and type)? | 5 === 5 | true |
!== | Not exactly equal? | 5 !== 3 | true |
Read it as a question: left side, then the sign, then the right side.
Working it out step by step
const score = 15;
console.log(score % 2 === 0);- Replace the variable:
15 % 2 === 0 - Do the maths first:
15 % 2is1, so1 === 0 - Answer the question: is 1 equal to 0? No.
- Result:
false
Maths always happens before comparison.
The program shows false, not a word like "odd". A comparison only ever produces true or false.
Saving the result
const age = 20;
const canVote = age >= 18;
console.log(canVote); trueThis is clean and common. Name the variable like a yes/no question: canVote, isEmpty, hasAccess.
Comparing strings
Strings are compared character by character, by their character codes. For simple English letters this is like alphabetical order, but with traps:
"apple" < "banana" true
"Zebra" < "apple" true (all capital letters come before small letters)
"10" < "9" true (compared as text: "1" comes before "9")
10 < 9 false (compared as numbers)- Convert text to numbers before comparing numbers from forms.
- For proper alphabetical sorting in real apps (including accents and capitals), use
a.localeCompare(b). Part 5 covers this with sorting.
Comparing different types
"10" > 9 trueWhen one side is a number and the other a string, > and < convert the string to a number. This "automatic conversion" is called type coercion (Part 5). It is safer to convert yourself first so the code is clear:
Number("10") > 9Comparisons with null and undefined
null >= 0 true
null > 0 false
null == 0 false
undefined > 0 falseThese results are confusing and are sometimes used as trick questions. The practical lesson: check for null and undefined before comparing.
Common mistakes
- Writing
TrueorFalsewith capitals. - Expecting a comparison to print a word instead of
trueorfalse. - Comparing numbers stored as strings:
"10" < "9". - Writing
=>(that is an arrow function) instead of>=. - Writing
5 < x < 10to check a range. It does not work as expected. Writex > 5 && x < 10(Lesson 35).
How to say it in an interview
"Comparison operators return booleans. Relational operators like greater-than coerce strings to numbers when compared with a number, but two strings are compared character by character, so '10' less than '9' is true. I convert inputs to numbers explicitly before comparing, and I use localeCompare for real alphabetical sorting."
Key words
| Word | Meaning |
|---|---|
| Boolean | A value that is true or false |
| Comparison operator | A sign that asks a question, like > or === |
| Expression | Code that produces a value, like 5 > 3 |
| Type coercion | JavaScript automatically converting a value to another type |
| localeCompare | A string method for correct alphabetical comparison |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.