Conditions

Make a decision

Lesson 4 of 20 · 7 min · Module 2: Decisions & reusable work

Great programs make decisions. An if statement runs a block of code only when its condition is true.

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Evaluate a comparison
  • Choose between branches
  • Return a result from a function

The idea

Use if and else to choose between two paths. A comparison like score >= 50 produces true or false.

const temperature = 24;
if (temperature > 20) {
  console.log("T-shirt weather!");
} else {
  console.log("Grab a jacket.");
}

01Read a boolean condition

age >= 18 is either true or false. The equality boundary matters: an age of exactly 18 must take the Yes branch.

console.log(18 >= 18);
console.log(17 >= 18);
Output
true
false

02Understand the function wrapper

function canVote(age) defines reusable behavior; it does not run that behavior immediately. Calling canVote(20) supplies age = 20. return sends the chosen string back to the caller. The next module explores functions further.

Your turn

Create a function canVote(age) that returns "Yes" for ages 18 and above, and "Not yet" otherwise.

What the checks look for

  • Age 18 can vote
  • Age 16 cannot vote yet
  • Works with other ages
Try it in the editor →

Take this with you

A decision is correct only when its boundary cases match the intended rule.

Learn JavaScript Foundations for free

20 short lessons with real code and instant checks.

See the whole course →