Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

JavaScript Control Flow: Conditions and Loops

Lesson 19 of 34 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

JavaScript Control Flow: Conditions and Loops

Conditional Statements

if / else if / else

const score = 75;

if (score >= 90) {
    console.log("Grade A");
} else if (score >= 75) {
    console.log("Grade B");
} else if (score >= 60) {
    console.log("Grade C");
} else {
    console.log("Fail");
}
// Output: Grade B

Ternary Operator

const age = 20;
const status = age >= 18 ? "Adult" : "Minor";
console.log(status); // Adult

Switch Statement

const day = "Monday";

switch (day) {
    case "Monday":
    case "Tuesday":
        console.log("Weekday");
        break;
    case "Saturday":
    case "Sunday":
        console.log("Weekend");
        break;
    default:
        console.log("Midweek");
}

Loops

for Loop

for (let i = 0; i < 5; i++) {
    console.log(i);  // 0 1 2 3 4
}

// Reverse
for (let i = 5; i > 0; i--) {
    console.log(i);  // 5 4 3 2 1
}

while Loop

let count = 0;
while (count < 3) {
    console.log("Count: " + count);
    count++;
}

do...while Loop

let n = 0;
do {
    console.log(n);
    n++;
} while (n < 3);
// Executes at least once

for...of (Arrays)

const fruits = ["apple", "banana", "cherry"];
for (const fruit of fruits) {
    console.log(fruit);
}

for...in (Objects)

const person = { name: "Alice", age: 25, city: "Delhi" };
for (const key in person) {
    console.log(`${key}: ${person[key]}`);
}

Loop Control

for (let i = 0; i < 10; i++) {
    if (i === 3) continue; // Skip 3
    if (i === 7) break;    // Stop at 7
    console.log(i);        // 0 1 2 4 5 6
}

Loop Comparison

LoopBest For
forKnown iterations
whileUnknown iterations
do...whileAt least one execution
for...ofArrays, iterables
for...inObject properties
Key Takeaway: JavaScript has rich control flow. Use === in conditions, prefer for...of for arrays, and for...in for objects. Use break and continue to control loop execution.