edubun
English
1 / 13

6 / 12 · Intro

Booleans and comparisons

5 == "5" is true but 5 === "5" is false. The triple equals sign also checks that both sides are the same type.

Jump to the 12 practice questions

You will learn

  • Compare values and get true or false
  • Prefer === to == and know why
  • Combine answers with &&, || and !

Before this: Strings and template literals

1Explain

Comparisons give true or false

A comparison takes two values and produces a boolean. The six you will use most are below. Notice that "equal" is written with three equals signs, because a single = already means "store a value".

OperatorQuestionExampleResult
===equal?4 === 4true
!==not equal?4 !== 4false
<less than?3 < 8true
>greater than?3 > 8false
<=less than or equal?8 <= 8true
>=greater than or equal?7 >= 8false

2Explore

Compare two values

=== needs the same value and the same type; == converts first. Strings are compared letter by letter.

a
b
const a = 5;
const b = 5;
console.log(a === b, a == b, a < b);
Output
true true false

3Problem

3 and "3" with ===

What does this print?

console.log(3 === "3");

Hint

A number and a string are different types.

Solution

Answer: false

A number and a string are different types, so strict equality is false.

4Explain

Why === and not ==

JavaScript also has a double equals, ==, called loose equality. Before comparing, it tries to convert both sides to the same type, which produces results that are hard to predict: 5 == "5" is true, and so is 0 == "". The triple equals ===, strict equality, never converts. If the types differ, the answer is simply false. MDN and most style guides recommend === and !== for everyday code, and this course uses them throughout.

console.log(5 === 5);      // true
console.log(5 === "5");    // false
// loose equality, best avoided:
console.log(5 == "5");     // true
console.log("cat" !== "dog"); // true
Output
true
false
true
true

5Problem

3 and "3" with ==

What does this print?

console.log(3 == "3");

Hint

== turns "3" into 3 before comparing.

Solution

Answer: true

Loose equality converts "3" to 3 first. This is why === is preferred.

6Explain

&&, || and !

Real questions often have several parts. A ride may require a height of at least 120 cm and an age of at least 8. JavaScript joins boolean answers with logical operators. && (and) is true only when both sides are true. || (or) is true when at least one side is true. ! (not) flips a boolean, so !true is false.

height is 130 and age is 7: the height test is true and the age test is false, so && gives false and || gives true.

  1. const height = 130;
  2. const age = 7;
  3. console.log(height >= 120 && age >= 8); // false
  4. console.log(height >= 120 || age >= 8); // true
  5. console.log(!(age >= 8)); // true
Variables
Output
false
true
true

7Explore

The ride check

Find the one combination where && is true, and the one where || is false.

height
age
const height = 110;
const age = 7;
console.log(height >= 120 && age >= 8);
console.log(height >= 120 || age >= 8);
Output
false
false

8Problem

Between 10 and 20

What does this print?

const x = 12;
console.log(x > 10 && x < 20);

Hint

Check each side of && on its own.

Solution

Answer: true

12 is greater than 10 and less than 20, so both sides are true.

9Problem

Saturday or Sunday

Worked example. Is a day a weekend? day === "Sat" || day === "Sun". Notice that you must repeat day === on both sides. Writing day === "Sat" || "Sun" looks natural in English but does not do what you mean, because "Sun" on its own is not a comparison. Comparisons are worked out before && and ||, so height >= 120 && age >= 8 needs no extra parentheses, though adding them never hurts.

Which correctly checks that day is "Sat" or "Sun"?

Hint

Each side of || must be a whole comparison.

Solution

Answer: day === "Sat" || day === "Sun"

Each side of || must be a full comparison, and a day cannot be both at once, so && is wrong.

10Practice

Write canRide(height, age)

Return true when the rider is at least 120 tall and at least 8 years old, false otherwise. Each test calls your function and prints what it returns.

Tests
TestRuns after your codeExpected output
1console.log(canRide(130, 7));
false
2console.log(canRide(130, 8));
true
3console.log(canRide(119, 10));
false
4console.log(canRide(120, 8));
true
Your code
function canRide(height, age) {
  return false;
}
Hint

Both conditions must hold, so join them with &&. "At least" means >=.

A solution
function canRide(height, age) {
  return height >= 120 && age >= 8;
}

11Practice

Write isWeekend(day)

Return true when day is "Sat" or "Sun", and false for any other day.

Tests
TestRuns after your codeExpected output
1console.log(isWeekend("Sat"));
true
2console.log(isWeekend("Sun"));
true
3console.log(isWeekend("Mon"));
false
Your code
function isWeekend(day) {
  return false;
}
Hint

Write day === on both sides of ||.

A solution
function isWeekend(day) {
  return day === "Sat" || day === "Sun";
}

12Explain

Common mistakes

console.log(3 =< 4);
Error message
SyntaxError: Invalid left-hand side in assignment
  1. The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
  2. After the colon: what went wrong, usually with the name involved.
  3. The line number shows where the program stopped. A SyntaxError stops it before any line runs.

The operator is <=, with < first. =< is not JavaScript, so the program is rejected before it runs.

13

Summary

  • Comparisons produce true or false.
  • Use === and !==; == converts types and surprises people.
  • && needs both, || needs one, ! flips.
  • Repeat the full comparison on each side of || and &&.
  • Falsy values: false, 0, "", null, undefined, NaN.

Next unitMaking decisions with if and else in JavaScript
Full text · The whole lesson as one text

Questions with a yes or no answer

Programs constantly ask questions. Is the password long enough? Is the player out of lives? Is today a weekend? Each of these has one of two answers, and JavaScript represents them with the two boolean values, true and false. Booleans are named after George Boole, the nineteenth-century mathematician who worked out an algebra of true and false. On their own they are not very exciting, but they are what lets a program make decisions, which is the subject of the next unit.

Comparison operators

A comparison takes two values and produces a boolean. The six you will use most are below. Notice that "equal" is written with three equals signs, because a single = already means "store a value".

OperatorQuestionExampleResult
===equal?4 === 4true
!==not equal?4 !== 4false
<less than?3 < 8true
>greater than?3 > 8false
<=less than or equal?8 <= 8true
>=greater than or equal?7 >= 8false

Like arithmetic, a comparison is an expression, so you can print it or store it in a variable: const isAdult = age >= 18; puts true or false into isAdult depending on the age. Variable names for booleans often start with is, has or can, which makes the code read like a question.

Why === and not ==

JavaScript also has a double equals, ==, called loose equality. Before comparing, it tries to convert both sides to the same type, which produces results that are hard to predict: 5 == "5" is true, and so is 0 == "". The triple equals ===, strict equality, never converts. If the types differ, the answer is simply false. MDN and most style guides recommend === and !== for everyday code, and this course uses them throughout.

console.log(5 === 5);      // true
console.log(5 === "5");    // false
// loose equality, best avoided:
console.log(5 == "5");     // true
console.log("cat" !== "dog"); // true

Strings can be compared too. === checks that two strings have exactly the same characters, capitals included, so "Yes" === "yes" is false. The < and > operators compare strings character by character using their character codes, which is close to dictionary order for lower-case words: "apple" < "banana" is true. Capital letters come before all lower-case letters in that ordering, so "Zebra" < "apple" is also true.

Combining answers with &&, || and !

Real questions often have several parts. A ride may require a height of at least 120 cm and an age of at least 8. JavaScript joins boolean answers with logical operators. && (and) is true only when both sides are true. || (or) is true when at least one side is true. ! (not) flips a boolean, so !true is false.

const height = 130;
const age = 7;
console.log(height >= 120 && age >= 8);   // false
console.log(height >= 120 || age >= 8);   // true
console.log(!(age >= 8));                 // true

Worked example. Is a day a weekend? day === "Sat" || day === "Sun". Notice that you must repeat day === on both sides. Writing day === "Sat" || "Sun" looks natural in English but does not do what you mean, because "Sun" on its own is not a comparison. Comparisons are worked out before && and ||, so height >= 120 && age >= 8 needs no extra parentheses, though adding them never hurts.

One more idea you will meet later: when JavaScript needs a boolean but gets some other value, it treats a few values as false. These falsy values are false, 0, the empty string "", null, undefined and NaN. NaN means “Not a Number”. It is a special numeric value produced by some invalid numeric operations, such as Number("cat"), and it is falsy. Everything else, including "0" and "false" as strings, counts as true.

and && needs both true. or || needs one true. not ! flips the answer.

To check a combined condition, decide the answer to each comparison separately before joining them. For the ride example, consider a rider who meets the height rule but not the age rule. The height comparison is true and the age comparison is false. An AND condition rejects that rider because both rules are required. An OR condition would accept the rider, which changes the rule rather than merely shortening its code.

Key points
  • Comparisons produce true or false.
  • Use === and !==; == converts types and surprises people.
  • && needs both, || needs one, ! flips.
  • Repeat the full comparison on each side of || and &&.
  • Falsy values: false, 0, "", null, undefined, NaN.

Practice questions from Booleans and comparisons on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (2)
  • MDN Web Docs, JavaScript Guide (developer.mozilla.org/en-US/docs/Web/JavaScript/Guide, chapters Introduction, Grammar and types, Control flow, Loops and iteration, Functions, Expressions and operators, Numbers and strings, Indexed collections, Working with objects; checked 2026-10-17)
  • Ecma International, ECMA-262 ECMAScript 2026 Language Specification, 17th edition, June 2026 (tc39.es/ecma262 and ecma-international.org/publications-and-standards/standards/ecma-262, checked 2026-10-17).

Checked: 2026-10-17