Running Code Only When a Condition Holds
Introduction
Welcome to Making Decisions and Repeating Work in JavaScript! In the earlier courses, we gave JavaScript instructions and stored values that those instructions could use. The program followed every line in order. Now we will teach it to skip some lines when they do not apply.
We will learn how an if statement checks a condition, how braces mark the lines that depend on that condition, and how else gives the program a second path. By the end, we will decide whether the Corner Cafe can serve a customer based on whether the shop is open and whether any cups remain.
A Condition with if
A condition is an expression that answers a yes-or-no question. Comparisons such as stockCount === 0 produce either true or false. An if statement uses that answer to decide whether a block of code should run. Here is a small example:
When we run this program, the condition is false, so the first message is skipped and the final message still appears:
The condition goes in parentheses after if. The opening brace begins the block, and the closing brace ends it. The two spaces before the line inside the block are called indentation. Indentation does not change the answer, but it makes it easy to see which line belongs to the choice. We use two spaces consistently. JavaScript also allows an if to control one statement without braces, but in this course we keep braces around conditional blocks to group the statements and make the structure visible.
In this course, we use comparisons that produce boolean answers. JavaScript can also evaluate other values in an if; we will leave those truthy and falsy cases for later. Equality checks like stockCount === 0 ask whether two values match, while comparisons like stockCount > 20 ask whether one number is larger. The result is decided before JavaScript enters the block. What matters is that the parentheses contain a complete expression and the braces contain the statements controlled by it.
If we change the count to zero, the same condition becomes true. The message inside the braces runs before the line after the block:
Now the output includes both messages:
The final console.log is outside the braces, so it runs whether the condition is true or false. Keeping braces around each conditional block groups its statements and clearly shows where the block ends.
The if block can hold more than one statement. If the cafe needs to print a warning and a suggested action, both lines can sit inside the same braces and both run together when the condition is true. We should keep the statements for one outcome inside its block, then close the brace before continuing with code that should always run. This boundary helps us predict the whole program: check the condition, run or skip the block, then continue after it.
Choosing Between Two Paths with else
Sometimes we want one of two messages to appear. We can add an else block after the first block. JavaScript runs the if block when the condition is true and the else block when it is false. Exactly one block runs:
The count is below ten, so the first branch runs and the other is skipped:
An else has no condition of its own. It means “if the condition above was false.” If we set stockCount to 14, the if condition is false, and the output changes to Stock level is healthy. No matter which number we choose, one of the two branches runs.
The else must stay paired with its if; it cannot stand by itself. We place it immediately after the closing brace of the if block, then open its own braces for the second outcome. When reading the code, we can use indentation as a visual guide: the two blocks line up, which tells us they are alternatives at the same level. If a message belongs after either outcome, we place that print after both blocks rather than inside either one.
Combining Values in a Condition
We can use a condition we already know how to write, including one combined with &&. The shop can serve a customer only when it is open and there is at least one cup available. Here the shop is open, but stock has reached zero:
One part of the condition is false, so the else branch runs. The final line is outside the choice and always appears:
If the stock count were positive, both parts would be true and the serving message would appear instead. A closed shop would also take the else branch even with stock available. This is the same && rule we learned earlier: both sides must be true for the whole condition to be true.
We can test the condition with a small table in our heads. Open with stock available means true and the serving message; closed with stock available means false and the unavailable message; open with no stock also means false. This reasoning helps us check the rule before we run the program. The following practice uses a positive stock count, so it exercises the other branch.
Reading the Whole Program
Here is the complete example together, with one single-condition choice and then a two-way choice:
When we click Run, we see:
The first block is skipped because four is not zero. In the second choice, four is less than ten, so its first branch runs. Then the program continues with the line after the choice.
Conclusion and Next Steps
We have taught a program to choose. An if statement checks a condition in parentheses and runs its braced block only when the answer is true. Adding else gives a second block that runs when the condition is false. Indenting the statements by two spaces helps us see which lines belong to each choice.
In the next lesson, we will make more than two choices with else if. We will see how JavaScript checks those conditions in order and why the order can change the result.
