1. Introduction
The if-else statement is the most fundamental decision-making tool in JavaScript. It lets a program execute one block of code when a condition is true, and a different block when it is false. Almost every non-trivial program needs to branch based on data — validating user input, checking permissions, or choosing between two calculations — and if-else is the building block for all of that logic.
Cricket analogy: An umpire deciding out or not out based on a DRS review is exactly an if-else branch — if the ball-tracking shows impact within the stumps, give it out; else, not out — the fundamental decision tool underlying every close call.
JavaScript evaluates the condition inside the parentheses as a boolean. If the condition is truthy, the if block runs; otherwise, control moves to an else if block (if present) or the final else block. You can chain as many else if clauses as needed to handle multiple distinct cases.
Cricket analogy: A match referee checks conditions in order — if rain persists, abandon; else if the ground is unfit, delay; else, play resumes — chaining multiple else-if branches just like an umpire's decision tree for weather.
2. Syntax
if (condition1) {
// runs when condition1 is truthy
} else if (condition2) {
// runs when condition1 is falsy and condition2 is truthy
} else {
// runs when all conditions above are falsy
}
// Single-statement bodies can omit braces (not recommended for readability)
if (isReady) console.log('Go!');
// Ternary operator as a compact alternative for simple if-else
const label = age >= 18 ? 'adult' : 'minor';3. Explanation
The condition in an if statement does not have to be a literal boolean — JavaScript coerces it to true or false using its internal ToBoolean rules. Any value that is not one of the falsy values is treated as truthy.
Cricket analogy: A selector treats any player who isn't officially injured, suspended, or out of form as available by default — everyone else, even with a shaky recent record, still counts as selectable, mirroring how JavaScript treats any value that isn't one of the specific falsy values as truthy.
else if is simply syntactic convenience: it is really an else block whose body is another if statement. Only the first matching branch executes; once a condition is true, JavaScript skips all the remaining else if / else branches in that chain, even if a later condition would also be true.
Cricket analogy: Once the third umpire confirms a catch is clean on review, the appeal process stops right there — nobody checks whether the batter was also short of his crease, just as JavaScript stops evaluating else-if branches the instant one condition matches.
There are exactly six falsy values in JavaScript: false, 0 (and -0), '' (empty string), null, undefined, and NaN. Every other value — including '0', 'false', [], and {} — is truthy. A very common bug is checking if (someArray.length) and forgetting that an empty array is truthy even though its length (0) is falsy; always compare the value you actually intend to test, e.g. if (someArray.length > 0).
4. Example
function classify(score) {
if (score >= 90) {
return 'A';
} else if (score >= 80) {
return 'B';
} else if (score >= 70) {
return 'C';
} else {
return 'F';
}
}
console.log(classify(95));
console.log(classify(82));
console.log(classify(55));
const input = '';
if (input) {
console.log('input has content');
} else {
console.log('input is empty or falsy');
}5. Output
A
B
F
input is empty or falsy6. Key Takeaways
- if-else lets a program choose between alternative code paths based on a condition.
- Conditions are coerced to boolean using truthy/falsy rules, not just literal true/false.
- Only false, 0, '', null, undefined, and NaN are falsy — everything else is truthy.
- else if chains stop at the first true condition; later branches are skipped even if also true.
- The ternary operator (condition ? a : b) is a concise alternative for simple two-way branches.
- Always use braces {} for if/else bodies to avoid bugs when adding more statements later.
Practice what you learned
1. Which of the following values is truthy in JavaScript?
2. What does the following code print? if (0) { console.log('yes'); } else { console.log('no'); }
3. In an if / else if / else if / else chain, what happens once one condition evaluates to true?
4. What is the value of result? const result = (5 > 3) ? 'big' : 'small';
5. Why is `if (arr.length)` risky when you actually mean 'array has at least one element'?
6. What does typeof NaN return, and how does it behave in an if condition?
Was this page helpful?
You May Also Like
switch Statement in JavaScript
Learn how the switch statement compares a value against multiple cases using strict equality, and why break is essential to prevent fall-through.
Operators in JavaScript
Survey JavaScript's arithmetic, comparison, logical, and ternary operators and how they behave with type coercion.
for Loop in JavaScript
Master the classic for loop along with for...in and for...of, and understand the key differences between iterating indexes, keys, and values.
break and continue in JavaScript
Learn how to alter loop execution with break and continue, and how labeled statements let you control outer loops from inside nested loops.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics