Course curriculum
JavaScript · LESSON 3 OF 5

Conditions and boolean logic

Choose between branches and combine conditions with explicit boolean rules.

About 8 minutes · Beginner

Choose one outcome

An if statement checks a condition. Its block runs when the condition is truthy; else provides an alternative. Braces group statements and help avoid mistakes when adding more lines.

Check specific thresholds before broader ones. A score of 85 satisfies both >= 75 and >= 40, so branch order matters.

JavaScript
const score = 72;
if (score >= 75) {
  console.log("Distinction");
} else if (score >= 40) {
  console.log("Pass");
} else {
  console.log("Try again");
}
Expected output
Pass

Combine requirements

&& requires both conditions to be truthy; || accepts either. ! negates truthiness. Prefer explicit comparisons when the requirement is numeric.

Values such as 0, an empty string, null and undefined are falsy. The string "false" is non-empty and therefore truthy; parse external values deliberately.

JavaScript
const age = 20;
const hasId = true;
console.log(age >= 18 && hasId);
console.log(Boolean("false"));
Expected output
true
true

Test the edges

For a pass mark of 40, check 39, 40 and 41. Also test invalid values if your function accepts user input.

Avoid assignments inside conditions. if (score = 40) changes score; if (score === 40) compares it.

Try it yourself

Print “Ready” only when completed is at least 3 and submitted is true.

Show a possible solution
JavaScript
const completed = 4;
const submitted = true;
if (completed >= 3 && submitted) {
  console.log("Ready");
}

Keep exploring: official JavaScript documentation.

0 of 5 completed on this device