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.
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".
| 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 |
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"); // trueStrings 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)); // trueWorked 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. Everything else, including "0" and "false" as strings, counts as true.
and && needs both true. or || needs one true. not ! flips the answer.
| boolean | true or false |
| === | Strict equality: same value and same type |
| !== | Strict inequality: not the same |
| == | Loose equality: converts types first (avoid) |
| && | and: true only if both sides are true |
| || | or: true if at least one side is true |
| ! | not: flips true and false |
| falsy | false, 0, "", null, undefined, NaN |
- What does this print?
console.log(7 > 7);- true
- false
- What does this print?
console.log(7 >= 7);- true
- false
- What does this print?
console.log(3 === "3");- true
- false
- What does this print?
console.log(3 == "3");- true
- false
- What does this print?
console.log(true && false);- true
- false
- What does this print?
console.log(true || false);- true
- false
- What does this print?
const x = 12; console.log(x > 10 && x < 20);- true
- false
- What does this print?
console.log(!(5 > 2));- true
- false
- What does this print?
console.log("Yes" === "yes");- true
- false
- What does this print?
console.log("apple" < "banana");- true
- false
- Which value is falsy?
"0"0"false"
- Which correctly checks that day is "Sat" or "Sun"?
day === "Sat" || "Sun"day === "Sat" || day === "Sun"day === "Sat" && day === "Sun"
- 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.