JavaScript Control Flow

Control the direction of execution using conditions and decisions.

if/elsenested logicboolean checks

Table 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.

JavaScript if statement flow chart showing true and false decision branches
Decision flow using JavaScript 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 if for a single condition.
  • Use if...else for two choices.
  • Use else if ladder for multiple conditions.
  • Use nested if when 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:

Atrue
Bfalse
Cnull
DNaN
Explanation: if block runs for truthy condition.
2

Which is correct else-if syntax?

Aelse (x)
Belse if (x)
Cif else (x)
Delseif x
Explanation: JavaScript uses else if(condition).
3

Ternary returns:

ATwo values based on condition
BAlways true
COnly false
DNo output
Explanation: Ternary selects one of two expressions.
4

Nested if means:

Aif inside another if
BTwo loops
COne switch
DNo conditions
Explanation: nested if is condition inside another condition.
5

Best equality operator for conditions?

A==
B===
C=
D!=
Explanation: strict equality avoids coercion bugs.
6

Falsy value among options:

A"0"
B[]
C0
D{}
Explanation: numeric 0 is falsy.
7

Guard clause usually uses:

Areturn early
Bdeep nesting
Cglobal var
Ddo-while
Explanation: guard clause exits function early.
8

Logical AND short-circuits on:

Afirst truthy
Bfirst falsy
Clast value always
Dnever
Explanation: && returns first falsy or last truthy.
9

else block runs when:

Aif condition true
Ball prior conditions false
Calways first
Dinside loop only
Explanation: else is fallback when conditions fail.
10

Which keyword is commonly used to skip the rest of a loop iteration inside condition logic?

Abreak
Bcontinue
Cdefault
Dswitch
Explanation: continue skips current iteration and moves to next loop cycle.