Home/Tutorials/JavaScript/Conditionals: if, else, switch

0 of 3 saved

JavaScript5 min read

Conditionals: if, else, switch

Read this chapter, write the program on this page, then pass a short check to open the next one.

Branch your program.

Conditionals: if, else, switch

Use if/else for most branches. switch is fine for many discrete cases. Ternary ? : is for short expressions.

  • Braces {} keep blocks clear.
  • switch needs break (or return) to avoid fall-through.
  • Prefer early returns over deep nesting.
const score = 85;
if (score >= 80) {
  console.log("pass");
} else {
  console.log("retry");
}

After you read this chapter, use Write (when available) and Quiz on this page to lock it in.