Unit 1: JavaScript Foundations & SyntaxLesson #2 / 10

Lesson 2: Control Flow, Conditionals & Loops

Er. Manoj Kumar — AuthorEr. Manoj KumarLast Updated: 26 Aug, 2026

1. Conditional Branching in JavaScript

Control flow statements allow your code to make logical decisions based on Boolean expressions.

  • if / else if / else: Evaluates conditional blocks sequentially.
  • Ternary Operator (condition ? exprIfTrue : exprIfFalse): Concise inline conditional assignments.
  • Switch Statement: Evaluates multiple discrete match cases against an expression.
  • HTML5 Web Code (index.html)
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>JS Control Flow</title>
    </head>
    <body style="background:#17181c; color:#fff; font-family:Arial; padding:30px;">
    <h2>JavaScript Loops & Decisions</h2>
    <ul id="number-list" style="list-style:none; padding:0;"></ul>
    <script>
    const marks = 85;
    let grade = "";
    if (marks >= 90) {
    grade = "A+ (Outstanding)";
    } else if (marks >= 75) {
    grade = "A (Excellent)";
    } else {
    grade = "B (Good)";
    }
    const listElement = document.getElementById("number-list");
    // Standard for loop
    for (let i = 1; i <= 5; i++) {
    const li = document.createElement("li");
    li.style.padding = "6px 12px";
    li.style.margin = "4px 0";
    li.style.backgroundColor = "#1f2029";
    li.style.borderLeft = "3px solid #04AA6D";
    li.textContent = "Step #" + i + " - Grade: " + grade;
    listElement.appendChild(li);
    }
    </script>
    </body>
    </html>

    2. Loop Types Comparison

  • for (let i = 0; i < n; i++): Index-based counter loop.
  • while (condition): Loops continuously while a condition remains true.
  • for...of: Iterates cleanly over array elements.
  • for...in: Iterates over object keys.
  • Interactive Knowledge Check

    Test your understanding of Lesson #2 concepts

    Why should developers always use === (strict equality) instead of == (loose equality)?