Course curriculum
Conditions and boolean logic
Choose between branches and combine conditions with explicit boolean rules.
About 8 minutes · BeginnerChoose 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.
const score = 72;
if (score >= 75) {
console.log("Distinction");
} else if (score >= 40) {
console.log("Pass");
} else {
console.log("Try again");
}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.
const age = 20;
const hasId = true;
console.log(age >= 18 && hasId);
console.log(Boolean("false"));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
const completed = 4;
const submitted = true;
if (completed >= 3 && submitted) {
console.log("Ready");
}Keep exploring: official JavaScript documentation.