What a branch does
A program does not always need to take the same path. It might show a welcome message when a name is available, or ask for a name when it is not. A branch lets the program choose which instructions to run based on a condition. In JavaScript, if starts that choice: when its condition is true, the code inside its braces runs. When the condition is false, JavaScript skips that code.
You can try the examples here in the developer console of a modern web browser. No library, project setup, or particular browser version is required; the examples use basic JavaScript syntax. Open the console, enter one example at a time, and look for its output. If you are new to the console, remember that console.log writes a message there; it does not put text on the web page.
Start with if, then add else
Suppose a program should respond when a visitor has completed a lesson. Enter: let completed = true; if (completed) { console.log("Lesson complete"); } Because completed is true, the message appears. Change true to false and run the whole example again. This time, the message does not appear. An if statement by itself is useful when no action is needed for the false case.
If both outcomes need an action, add else: let completed = false; if (completed) { console.log("Lesson complete"); } else { console.log("Keep going"); } This prints “Keep going.” The else block has no condition of its own: it runs only when the preceding if condition is false. For this two-way choice, exactly one of the two blocks runs. Try switching completed to true to confirm which message appears.
Choose among several paths with else if
Use else if when there are more than two possible outcomes. For example: let temperature = 19; if (temperature >= 25) { console.log("Warm"); } else if (temperature >= 15) { console.log("Mild"); } else { console.log("Cool"); } This prints “Mild.” JavaScript checks the conditions from top to bottom. The first condition is false because 19 is not at least 25. The second is true, so its block runs and the remaining else block is skipped.
The order matters. A temperature of 28 satisfies both temperature >= 25 and temperature >= 15, but only “Warm” prints because the first matching branch wins. Putting the broader test, temperature >= 15, first would make the “Warm” branch unreachable for temperatures of 15 or more. Think of an else if chain as a sequence of questions: ask the most specific or highest-priority question first, then move to the next only if the answer is no. You can add more else if blocks when needed, and the final else remains optional.
Write conditions that match your intent
A condition should express the decision you mean to make. Comparison operators include >, <, >=, and <=. The equals sign by itself, =, assigns a value; it does not compare values. For equality checks, prefer === in beginner JavaScript examples because it checks both value and type. For instance, 5 === "5" is false: one is a number and the other is text. To check whether a value is different, use !==.
You can combine conditions when a decision depends on more than one fact. For example, if (hasAccount && isSignedIn) runs its block only when both values are true. With ||, either condition may be true. Keep combined conditions readable: if a line is hard to explain aloud, consider giving part of the condition a descriptive variable name. Also note that JavaScript can treat values such as 0 and an empty string as false in a condition. An explicit comparison is often clearer when those values have a particular meaning in your program.
Check boundaries and debug the path taken
Test values near each cutoff, not just obvious examples. In the temperature example, try 14, 15, 24, and 25. These values reveal whether >= expresses the boundary you intended. If 25 should still count as mild, the first condition needs to change. Writing down the expected message before running the code makes it easier to spot a mistake rather than accepting whatever appears.
When a branch behaves unexpectedly, inspect both the value and the condition. A console.log before the if chain can show the value being tested. Then read the conditions in order and stop at the first one that evaluates to true. Check for a misplaced else, an incorrect comparison operator, or text being compared with a number. Keeping each branch small and its message distinctive makes the path easier to follow while you learn.



