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 questionsYou 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".
| Operator | Question | Example | Result |
|---|---|---|---|
| === | equal? | 4 === 4 | true |
| !== | not equal? | 4 !== 4 | false |
| < | less than? | 3 < 8 | true |
| > | greater than? | 3 > 8 | false |
| <= | less than or equal? | 8 <= 8 | true |
| >= | greater than or equal? | 7 >= 8 | false |
2Explore
Compare two values
=== needs the same value and the same type; == converts first. Strings are compared letter by letter.
const a = 5;
const b = 5;
console.log(a === b, a == b, a < b);true true false3Problem
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"); // truetrue
false
true
true5Problem
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.
const height = 130;const age = 7;console.log(height >= 120 && age >= 8); // falseconsole.log(height >= 120 || age >= 8); // trueconsole.log(!(age >= 8)); // true
false true true
7Explore
The ride check
Find the one combination where && is true, and the one where || is false.
const height = 110;
const age = 7;
console.log(height >= 120 && age >= 8);
console.log(height >= 120 || age >= 8);false
false8Problem
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.
| Test | Runs after your code | Expected output |
|---|---|---|
| 1 | console.log(canRide(130, 7)); | false |
| 2 | console.log(canRide(130, 8)); | true |
| 3 | console.log(canRide(119, 10)); | false |
| 4 | console.log(canRide(120, 8)); | true |
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.
| Test | Runs after your code | Expected output |
|---|---|---|
| 1 | console.log(isWeekend("Sat")); | true |
| 2 | console.log(isWeekend("Sun")); | true |
| 3 | console.log(isWeekend("Mon")); | false |
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);SyntaxError: Invalid left-hand side in assignment
- The word before the colon names the kind of error: ReferenceError, TypeError, SyntaxError.
- After the colon: what went wrong, usually with the name involved.
- 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.