Skip to the content

Lesson 33 of 296

Booleans and comparison signs

What you will learn

  • What booleans are for
  • The comparison operators
  • How comparisons produce true or false
  • 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.

OperatorQuestionExampleResult
>Is the left bigger than the right?8 > 5true
<Is the left smaller than the right?8 < 5false
>=Bigger or equal?5 >= 5true
<=Smaller or equal?4 <= 5true
===Exactly equal (value and type)?5 === 5true
!==Not exactly equal?5 !== 3true

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);
  1. Replace the variable: 15 % 2 === 0
  2. Do the maths first: 15 % 2 is 1, so 1 === 0
  3. Answer the question: is 1 equal to 0? No.
  4. 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);      true

This 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        true

When 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") > 9

Comparisons with null and undefined

null >= 0        true
null > 0         false
null == 0        false
undefined > 0    false

These results are confusing and are sometimes used as trick questions. The practical lesson: check for null and undefined before comparing.

Common mistakes

  • Writing True or False with capitals.
  • Expecting a comparison to print a word instead of true or false.
  • Comparing numbers stored as strings: "10" < "9".
  • Writing => (that is an arrow function) instead of >=.
  • Writing 5 < x < 10 to check a range. It does not work as expected. Write x > 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

WordMeaning
BooleanA value that is true or false
Comparison operatorA sign that asks a question, like > or ===
ExpressionCode that produces a value, like 5 > 3
Type coercionJavaScript automatically converting a value to another type
localeCompareA string method for correct alphabetical comparison

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.