You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Control flow determines the order in which statements are executed. By using conditionals and loops, you can make your programs react to different inputs and repeat tasks efficiently.
The most fundamental conditional statement:
const temperature = 28;
if (temperature > 30) {
console.log("It's hot!");
} else if (temperature > 20) {
console.log("It's warm.");
} else if (temperature > 10) {
console.log("It's cool.");
} else {
console.log("It's cold!");
}
// Output: "It's warm."
if() is converted to a boolean.else if and else are optional.JavaScript converts any value to a boolean when needed. Values that convert to false are called falsy:
| Falsy Values |
|---|
false |
0, -0, 0n |
"" (empty string) |
null |
undefined |
NaN |
Everything else is truthy — including "0", "false", empty arrays [], and empty objects {}.
if ("hello") {
console.log("Truthy!"); // this runs
}
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.