JavaScript, lesson 4
So far every line of your programs has run. Real programs choose: show a warning only when
it is cold, charge less for children, end the game when lives run out. JavaScript makes those
choices with conditions: expressions that are either true or false.
if, else if, elseJavaScript checks each condition from the top and runs the first block whose condition is
true, then skips the rest. The else block runs when nothing above matched. The braces group
the statements that belong to each branch.
| Operator |
|---|
2 exercises to try what you just read.
| Meaning |
|---|
=== | equal (same type and value) |
!== | not equal |
<, <= | less than, less than or equal |
>, >= | greater than, greater than or equal |
Always use the triple-equals forms. The older == and != convert types before comparing, so
"5" == 5 and 0 == "" are both true, which hides bugs. With ===, "5" === 5 is false,
because a string is never equal to a number.
Strings compare alphabetically by character code: "apple" < "banana" is true, but so is
"Zebra" < "apple", because uppercase letters come before lowercase ones.
Combine conditions with:
&& (and): true when both sides are true.|| (or): true when at least one side is true.! (not): flips true and false.&& and || short-circuit: if the left side already decides the answer, the right side is
never evaluated.
An if accepts any value, not just booleans. These values count as false and are called
falsy: false, 0, "" (empty string), null, undefined and NaN. Everything else is
truthy, including "0" and "false" (non-empty strings). That makes if (name) a quick
"is the string non-empty?" check, but be careful with numbers where 0 is a valid value.
For a choice between two values, the conditional (ternary) operator is compact:
Read it as "condition ? value if true : value if false".
switchWhen you compare one value against many exact options, switch reads well:
switch uses === to match. Each case needs a break, otherwise execution "falls through"
into the next case. Falling through on purpose, as with "sat" and "sun" above, lets two
cases share one block.
Read a temperature in degrees Celsius (a whole number, possibly negative) and print one word of advice:
0: coat0 up to and including 15: jacket16 up to and including 25: t-shirt25: shortsSample input:
Sample output:
A year is a leap year when it is divisible by 4, except years divisible by 100, which are leap years only when they are also divisible by 400. So 2024 and 2000 are leap years, but 1900 is not.
Read a year and print YEAR is a leap year or YEAR is not a leap year.
Sample input:
Sample output:
const temp = 4;
if (temp < 0) {
console.log("Freezing");
} else if (temp < 15) {
console.log("Cold");
} else {
console.log("Warm");
}const age = 15;
const withAdult = true;
if (age >= 18 || withAdult) {
console.log("You may enter");
}const items = 1;
console.log(`${items} ${items === 1 ? "item" : "items"}`);const day = "sat";
switch (day) {
case "sat":
case "sun":
console.log("Weekend");
break;
case "fri":
console.log("Almost there");
break;
default:
console.log("Weekday");
}18t-shirt20242024 is a leap year