Control Flow
if / else if / else
let score = 82;
if (score >= 90) {
console.log("A");
} else if (score >= 80) {
console.log("B"); // ← this runs
} else if (score >= 70) {
console.log("C");
} else {
console.log("F");
}
switch Statement
let day = "Monday";
switch (day) {
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
console.log("Weekday");
break; // IMPORTANT: without break, falls through!
case "Saturday":
case "Sunday":
console.log("Weekend");
break;
default:
console.log("Unknown day");
}
Always add
breakunless fall-through is intentional.
for Loop
// Classic for loop
for (let i = 0; i < 5; i++) {
console.log(i); // 0, 1, 2, 3, 4
}
// for...of loop (arrays, strings, iterables)
const fruits = ["apple", "banana", "cherry"];
for (const fruit of fruits) {
console.log(fruit);
}
// for...in loop (object keys)
const person = { name: "Reet", age: 25, city: "Rajkot" };
for (const key in person) {
console.log(`${key}: ${person[key]}`);
}
while and do...while
// while: checks condition FIRST
let count = 0;
while (count < 5) {
console.log(count);
count++;
}
// do...while: executes AT LEAST ONCE, then checks condition
let num = 10;
do {
console.log(num); // runs once even though condition is false
num++;
} while (num < 5);
// Output: 10
break and continue
// break: exits the loop entirely
for (let i = 0; i < 10; i++) {
if (i === 5) break;
console.log(i); // 0, 1, 2, 3, 4
}
// continue: skips current iteration
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) continue; // skip even numbers
console.log(i); // 1, 3, 5, 7, 9
}
// Labeled break (for nested loops)
outer: for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (j === 1) break outer;
console.log(i, j);
}
}
// Output: 0 0
Truthy and Falsy Values
// Falsy values (6 total):
// false, 0, "", null, undefined, NaN
if (0) { } // won't run
if ("") { } // won't run
if (null) { } // won't run
if (undefined) { } // won't run
if (NaN) { } // won't run
// Truthy (everything else):
if (1) console.log("runs");
if ("hello") console.log("runs");
if ([]) console.log("runs"); // empty array is truthy!
if ({}) console.log("runs"); // empty object is truthy!
if (-1) console.log("runs"); // negative numbers are truthy!
forEach, map, filter, reduce (Array iteration)
const nums = [1, 2, 3, 4, 5];
nums.forEach(n => console.log(n)); // iterate, no return value
nums.map(n => n * 2); // [2,4,6,8,10] — new array
nums.filter(n => n % 2 === 0); // [2,4] — filtered array
nums.reduce((acc, n) => acc + n, 0); // 15 — single value
MCQ — Control Flow
Q1: What happens if you forget break in a switch case?
A) The switch exits automatically after each case B) Code falls through to the next case ✅ C) A SyntaxError is thrown D) The default case runs
Answer: B — Without break, execution continues into the next case (fall-through).
Q2: What does a do...while loop guarantee?
A) Nothing is logged if condition is false
B) The body executes at least once ✅
C) An infinite loop
D) undefined
Answer: B — do...while always executes at least once before checking the condition.
Q3: Which loop is best for iterating over object properties?
A) for...of
B) while
C) for...in ✅
D) forEach
Answer: C — for...in iterates over enumerable object keys.
Q4: What does continue do in a loop?
A) Exits the loop B) Exits the function C) Skips the current iteration and goes to the next ✅ D) Restarts the loop
Answer: C
Q5: Is [] truthy or falsy in JavaScript?
A) Falsy B) Truthy ✅ C) Error D) Depends on context
Answer: B — Empty arrays and empty objects are truthy! Only the 6 falsy values are falsy.
Q6: What is the difference between for...of and for...in?
A) They are identical
B) for...of iterates values; for...in iterates keys ✅
C) for...in iterates values; for...of iterates keys
D) for...of works only on objects
Answer: B
Q7: How many falsy values does JavaScript have?
A) 4 B) 5 C) 6 ✅ D) 7
Answer: C — false, 0, "", null, undefined, NaN.
Q8: What does this print?
for (let i = 0; i < 3; i++) {
if (i === 1) continue;
console.log(i);
}
A) 0, 1, 2 B) 0, 2 ✅ C) 1, 2 D) 0, 1
Answer: B — When i === 1, continue skips that iteration.