Programming

conditional

A conditional is code that makes a decision. It checks whether something is true, and based on the answer it picks which path to take: IF this is true, do that — OTHERWISE, do something else. This is how programs choose.

It works like a fork in the road. The program reaches the fork, asks a yes/no question — is the user logged in? is the cart empty? — and goes left or right depending on the answer. Same code, different outcomes for different situations.

You can chain the questions: if the first isn't true, check a second (else if), and if none match, fall through to a final catch-all. That's how a handful of simple yes/no checks add up to behaviour that feels smart.

if (score >= 60) {
  console.log("Pass")
} else {
  console.log("Try again")
}

One yes/no check, two different paths — that's a conditional.

When you'd be writing many if/else if branches against one value, a 'switch' statement often reads cleaner.

Also called
if statementif/elsebranch
See also