thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

07.01 · If, else & else if / Learn · 3 min

Choose when code runs

Your program reaches a decision. Does it enter the block, or skip ahead?

Activity 1 of 7 in If, else & else if

All courses

JavaScript Basics / 07.01

Choose when code runs

Not started

Use an if statement to run a block only when its condition is truthy, then continue after the block.

More options

Your lesson progress

Reading & walkthrough: not startedGuided practice: not started

Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Rob inspects a gate and a branching track, then follows a token past the decision point.
Rob’s comic strip · If, else & else if

Explore the changing values

Move the control and follow the highlighted instruction. Compare the result with the explanation beside it.

Move the temperature across each boundary. Notice which single branch runs.

Your program output

This explorer uses fixed examples. Your saved practice code stays separate.

A comparison becomes a decision

You already know comparisons return true or false. Put one inside if (...) to decide whether the block inside braces runs.

If the condition is falsy, JavaScript skips that block. An if statement is not a loop: it does not keep checking while the input changes.

Read from top to bottom
example.jsjavascript · example
const number = 3;
if (number > 0) {
  console.log("Positive");
}
console.log("After check");

Keep the block visible

Use braces and indentation so the controlled lines are easy to see. A semicolon immediately after if (condition) would create an empty statement instead of controlling the following block.

Try number = 0, then -2. Both skip the Positive message, while After check still prints. Zero is not a positive or negative number.

The closing brace ends the controlled block
example.jsjavascript · example
const number = 0;
if (number > 0) {
  console.log("Positive");
}
console.log("After check"); // Still runs

Choose a message with if and else

Work through 4 focused steps. Read the explanation, type each requested change and check it before moving on. Your own code carries forward.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

4 steps · Saved work · Instant feedback

YOUR TURN

Try the examples on this page

Choose Load this code into the editor beside an example, then edit and Run code here. Predict what will print before running it. This is an exploration workspace, so there is no pass/fail challenge. Its saved work stays separate from the next practice activity.

Load an example, edit it and run it. Word wrap is enabled.

Work in VS Code instead

Copy the starter into a file named practice.js in VS Code. If Node.js is installed, open the terminal in that folder and run node practice.js. Compare the output with the task above. Automated challenge checks are available in the browser editor.

Code example
example.jsjavascript · example
const number = 3;
if (number > 0) {
  console.log("Positive");
}
console.log("After check");

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

The condition controls the block. Code after the block still runs.