← All topics

LESSON 04 OF 10

Conditions and Decisions

Choose a path with if, else, and comparisons.

Before this lesson: complete the preceding lessons or review their examples. No installation or account is needed.

The idea

A condition selects which code executes. if runs its block when its condition is truthy. else provides another path. Use comparisons to express the decision you actually want to make.

At a glance

SyntaxPurpose
if / elseSelect a branch
&&Require both conditions to be truthy
||Accept either truthy condition

Try it yourself

Change the score to test every branch, including the exact boundaries 60 and 90.

Ready. Run the example to open its preview.

Console output

Not run yet.

Page preview

Expected result
Keep practicing

What to watch for

Falsy values include false, 0, an empty string, null, undefined, and NaN. The string "false" is truthy. Test the most restrictive threshold first when checking descending score ranges.

Your turn

Print Ready when minutes is at least 20 and hasNotebook is true. Otherwise print Prepare first.

Show one solution
const minutes = 25;
const hasNotebook = true;
if (minutes >= 20 && hasNotebook) {
  console.log("Ready");
} else {
  console.log("Prepare first");
}

Check your understanding

Will both branches of an if/else run?

No. One branch is selected for that evaluation of the condition.

Read alongside this lesson

W3Schools: Conditions and Decisions · All learning sources