Syncing account progress…

Back

Beginner · 8 min

Make decisions

Choose which instructions run with if and else.

Run code conditionally

if runs its block when the condition is true. else handles the other case. Braces group the instructions in each block.

const stock = 3;
if (stock > 0) {
  console.log("Available");
} else {
  console.log("Sold out");
}

Output

Available

Handle another condition

else if checks another condition only when the earlier condition was false. Put your most specific condition first when conditions overlap.

const score = 85;
if (score >= 90) {
  console.log("Excellent");
} else if (score >= 70) {
  console.log("Passed");
} else {
  console.log("Try again");
}

Output

Passed

Put it into practice

  1. Predict the output of each example before running it.
  2. Complete the coding task, then try the practice questions.

Try it yourself

Code runs on this device. When you are signed in, drafts sync to your account.

Choose a delivery message

Display "Free delivery" when total is at least 20; otherwise display "Delivery fee".

Practice