What the or operator does
Sometimes code should run when at least one of two conditions holds. The logical or operator || is true if either side is true, and false only when both sides are false.
| left | right | left || right |
|---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
In 7 >= 10 || 10 < 12, the left side is false and the right side is true, so the whole expression is true. In 10 < 15 || 10 > 9, both sides are true, and the result is true as well. This operator is inclusive: having both conditions true is allowed.
Inside an if:
let x = 10;
if (x != 10 || 12 < x) {
console.log("This is true!");
}
Both x != 10 and 12 < x are false, so the condition is false and nothing is printed.
Short-circuit evaluation
The operator reads left to right and stops when the answer is known. If the left side is true, the whole expression must be true, and the right side is never evaluated:
let calls: number = 0;
function check(): boolean {
calls++;
return true;
}
console.log(true || check(), calls);
console.log(false || check(), calls);
true 0
true 1
The first call never runs check, so calls stays at 0. The second needs the right side, so it runs.
TypeScript details
||gives back one of its operands. With booleans, the result is a boolean. With other values,0 || "dflt"gives"dflt", because0counts as false. That makes||a common way to supply a default, but it also replaces valid values like0and"". The nullish operator??replaces onlynullandundefined:0 ?? "dflt"gives0.- Do not mix
||with??without parentheses.a || b ?? cis a syntax error in JavaScript, so the compiled program does not start at all. !=and!==. The lesson's solution compares strings with!=. Between two strings it gives the same answer as!==, which never converts types and is the safer habit.
The exercise
The code declares weather and temperature. The program must print "Learning to code" when the weather is not sunny or the temperature is 85 or below, and "Going to the beach" otherwise.
- Write the first comparison,
weather != "sunny", true when the weather is anything but sunny. - Write the second,
temperature <= 85, true when it is not hotter than 85. The comparison includes 85 itself, since only temperatures above 85 count as hot. - Join them with
||. Either problem is enough to stay home. - Log
"Learning to code"in theifblock, and add} else {with a log of"Going to the beach".
let weather: string = "sunny";
let temperature: number = 90;
if (weather != "sunny" || temperature <= 85) {
console.log("Learning to code");
} else {
console.log("Going to the beach");
}
With the starting values, the weather is sunny and the temperature is 90. Both comparisons are false, so the condition is false and the else runs:
Going to the beach
Change weather to "rainy" or temperature to 80 and the first message appears instead.
Why the else is the beach
The else block runs when the whole condition is false, and an or is false only when both sides are false: the weather is sunny and the temperature is above 85. This is the same situation that weather == "sunny" && temperature > 85 describes. The two forms are linked by De Morgan's laws: !(a && b) equals !a || !b. Negating == gives !=, and negating > gives <=, which is exactly what this solution writes.
Common mistakes
- Using
&&instead of||. The message would then appear only when both are problems: rainy and cold. - Using
<instead of<=. A temperature of exactly 85 would go to the beach instead of staying home. - Using a single
|. That is the bitwise or, which works on the bits of numbers, and it is not what you want for conditions. - Repeating only half the condition.
weather != "sunny" || "cold"is always true: when the weather is sunny, the expression gives"cold", and a non-empty string counts as true.
Key takeaways
a || bis false only when bothaandbare false.- If the left side is true, the right side is not evaluated.
- Use
??rather than||for defaults when0or an empty string is a valid value. - The
elseof an or-condition covers the case where every part is false.