JavaScript Control Flow
Control the direction of execution using conditions and decisions.
if/elsenested logicboolean checksTable of Contents
JavaScript Conditional Statements
Conditional statements let your program make decisions. In JavaScript, they control which block of code runs based on conditions that evaluate to true or false.

if and if...else statements.if Statement (Basic Decision)
Use if when you want to execute code only when a condition is true.
let age = 20;
if (age >= 18) {
console.log("You are eligible to vote");
}If the condition is false, nothing happens.
if...else Statement (Two Choices)
Use this when you have exactly two possible outcomes.
let age = 16;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}Here, one block will always execute.
else if Ladder (Multiple Conditions)
Use this when you need to check multiple conditions in sequence.
let marks = 75;
if (marks >= 90) {
console.log("Grade A");
} else if (marks >= 75) {
console.log("Grade B");
} else if (marks >= 50) {
console.log("Grade C");
} else {
console.log("Fail");
}JavaScript checks conditions from top to bottom and stops at the first true condition.
Nested if (if inside if)
Use nested if when a condition depends on another condition.
let age = 20;
let hasID = true;
if (age >= 18) {
if (hasID) {
console.log("Allowed to enter");
} else {
console.log("ID required");
}
} else {
console.log("Not allowed");
}5. Important Tips
- Always use
{ }even for single statements to avoid mistakes. - Be careful with comparison operators:
== // compares value
=== // compares value and type (recommended)
if (5 === "5") {
console.log("Equal");
} else {
console.log("Not Equal"); // This runs
}6. Real-Life Example
let username = "admin";
let password = "1234";
if (username === "admin") {
if (password === "1234") {
console.log("Login successful");
} else {
console.log("Wrong password");
}
} else {
console.log("User not found");
}7. When to Use What
- Use
iffor a single condition. - Use
if...elsefor two choices. - Use
else ifladder for multiple conditions. - Use nested
ifwhen conditions depend on each other.
8. Best Practices
- Keep conditions readable and avoid unnecessary deep nesting.
- Use strict checks (
===) where possible. - Extract repeated logic into functions.
- Prefer guard clauses for early exits in long functions.
9. 10 Control Flow Interview Q&A
1What is control flow?easy
Answer: Order in which statements execute based on conditions.
2Difference between if and else if?easy
Answer: else if checks additional condition after previous false branch.
3When to use ternary?easy
Answer: For short conditional value assignment.
4What are guard clauses?medium
Answer: Early return checks that reduce nested blocks.
5Why avoid deeply nested if blocks?easy
Answer: Harder to read, test, and maintain.
6Can non-boolean values be used in if?medium
Answer: Yes, JS evaluates truthy/falsy values.
7What is short-circuit logic in conditions?medium
Answer: && and || stop evaluation early once outcome is known.
8How to simplify multiple comparisons?medium
Answer: Use helper functions, switch, or object mapping as needed.
9Why use strict equality in conditions?easy
Answer: Prevents coercion-related bugs.
10What follows control flow topic next?easy
Answer: Switch statements and loops for multi-branch and repeated logic.
10. 10 Control Flow MCQs
1
if executes when condition is:
Explanation: if block runs for truthy condition.
2
Which is correct else-if syntax?
Explanation: JavaScript uses else if(condition).
3
Ternary returns:
Explanation: Ternary selects one of two expressions.
4
Nested if means:
Explanation: nested if is condition inside another condition.
5
Best equality operator for conditions?
Explanation: strict equality avoids coercion bugs.
6
Falsy value among options:
Explanation: numeric 0 is falsy.
7
Guard clause usually uses:
Explanation: guard clause exits function early.
8
Logical AND short-circuits on:
Explanation: && returns first falsy or last truthy.
9
else block runs when:
Explanation: else is fallback when conditions fail.
10
Which keyword is commonly used to skip the rest of a loop iteration inside condition logic?
Explanation:
continue skips current iteration and moves to next loop cycle.