if, else if, else Statements in JavaScript
An if statement runs its block when the condition is truthy; else if chains test alternatives in order and a trailing else catches everything left over. Conditions accept any value through truthiness β comparisons most often, but also direct values, lengths, and existence checks.
Braces are optional for single statements but omitting them invites dangling-else confusion when lines are added later. Early returns (guard clauses) flatten nested validation into a readable sequence of rejections before the real work.
Truthy runs the block
if (score >= 60) executes only when the comparison is truthy; any truthy value (nonzero, non-empty) qualifies.
else if chains alternatives
Each tests only when everything above failed β first-match-wins ordering for grades and ranges.
else catches the rest
A final else needs no condition and guarantees some path executes.
Guard early, brace always
if (!input) return; up front beats nesting; braces on every branch prevent future dangling-else bugs.
let score = 85;if (score >= 90) { console.log("A");} else if (score >= 80) { console.log("B");} else { console.log("C or below");}Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself β building beats watching.