What the and operator does
Sometimes one comparison is not enough. A trip to the beach needs sunny weather and a high temperature. The logical and operator && joins two conditions into one that is true only when both sides are true.
| left | right | left && right |
|---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
The comparisons on each side are evaluated first, and then && combines their results. In 10 == 10 && 7 < 10, the left side is true and the right side is true, so the whole expression is true.
Inside an if, it looks like this:
let x = 10;
if (x != 7 && 12 < x) {
console.log("This is true!");
}
Here x != 7 is true but 12 < x is false, so the whole condition is false, the block is skipped, and nothing is printed.
Each side must be a full condition
A common beginner slip is to write x > 5 && < 10 as if the sentence "x is greater than 5 and less than 10" could be copied into code, which is a syntax error. Each side of && must be a complete condition that stands on its own: x > 5 && x < 10. The same goes for ranges in general: a value is between two bounds when it is above the lower one and below the upper one.
Short-circuit evaluation
The operator reads left to right and stops as soon as the answer is known. If the left side is false, the whole expression must be false, so the right side is never evaluated. A function call on the right does not even happen:
let calls: number = 0;
function check(): boolean {
calls++;
return true;
}
console.log(false && check(), calls);
console.log(true && check(), calls);
false 0
true 1
This is more than an optimization. You can put a safety test on the left and the risky one on the right: text !== null && text.length > 3 never reads text.length when text is null.
TypeScript details
- Comparing with
==and===. The lesson's examples use==. Between two values of the same type, such as two numbers or two strings,==and===give the same answer.===is the safer habit because it never converts types. - The compiler checks comparisons. If a variable is declared with
const weather = "rainy", its type is the literal"rainy", and the compiler reports that comparing it with"sunny"has no overlap. Declared aslet weather: string = "rainy", as in this lesson, any string can be compared. The compiler still produces JavaScript, but the message is a hint that the condition can never be true. &&gives back one of its operands. With booleans on both sides the result is a boolean, as in this lesson. With other values,"a" && "b"gives"b", and"" && "b"gives"". Inside anif, only whether the result counts as true matters.
The exercise
The code declares weather and temperature. The task is to go to the beach only when the weather is "sunny" and the temperature is above 85, and otherwise to learn to code.
- Write an
ifwhose condition joins the two checks with&&:weatherequal to"sunny"andtemperaturegreater than85. - Log
"Going to the beach"inside that block, so the message appears only when both checks pass. - Add an
elseblock that logs"Learning to code". It runs when either check fails.
let weather: string = "rainy";
let temperature: number = 90;
if (weather == "sunny" && temperature > 85) {
console.log("Going to the beach");
} else {
console.log("Learning to code");
}
With the starting values, the temperature check is true but the weather check is false, so the program prints:
Learning to code
Change weather to "sunny" in the editor above and both checks pass, so the message becomes Going to the beach. Lower temperature to 85 and it goes back to Learning to code, because the comparison is strictly greater than.
Common mistakes
- Using one
&instead of two. A single&is a different operator, the bitwise and, which works on the bits of numbers. For conditions, write&&. - Using
=instead of==or===.weather = "sunny"assigns, and the condition no longer compares anything. - Getting the case or spelling of the text wrong.
"Sunny"is not equal to"sunny". - Using
>=when the task says above. A temperature of exactly 85 should not go to the beach.
Key takeaways
a && bis true only if bothaandbare true.- Each side of
&&must be a complete condition. - If the left side is false, the right side is not evaluated.
- Use
===by default, and useelseto cover everything theifdid not.