1. Conditional Statements
These are used to perform different actions based on different conditions.
if Statement
let age = 18;
if (age >= 18) {
console.log("You are eligible to vote.");
}
if...else Statement
let score = 45;
if (score >= 50) {
console.log("You passed!");
} else {
console.log("You failed.");
}
if...else if...else Statement
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");
}
switch Statement
Used when you have many conditions to check based on a single variable.
let day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the week");
break;
case "Friday":
console.log("End of the week");
break;
default:
console.log("Regular day");
}
2. Loops
Loops execute a block of code a number of times until a specified condition is false.
for Loop
for (let i = 1; i <= 5; i++) {
console.log("Count: " + i);
}
while Loop
let i = 1;
while (i <= 5) {
console.log("Count: " + i);
i++;
}
do...while Loop
This loop runs at least once even if the condition is false at the beginning.
let i = 1;
do {
console.log("Count: " + i);
i++;
} while (i <= 5);
for...of Loop
Used to iterate over iterable objects like arrays.
let fruits = ["apple", "banana", "cherry"];
for (let fruit of fruits) {
console.log(fruit);
}
for...in Loop
Used to iterate over the enumerable properties of an object.
let student = { name: "Alice", age: 20, grade: "A" };
for (let key in student) {
console.log(key + ": " + student[key]);
}
3. Control Flow Statements
break Statement
Exits the current loop or switch
statement.
for (let i = 1; i <= 10; i++) {
if (i === 5) break;
console.log(i);
}
continue Statement
Skips the current iteration and continues with the next one.
for (let i = 1; i <= 5; i++) {
if (i === 3) continue;
console.log(i);
}
return Statement
Used to exit a function and return a value.
function add(a, b) {
return a + b;
}
console.log(add(5, 3)); // 8
Best Practices
- Always use
break
inswitch
to prevent fall-through. - Use
const
orlet
instead ofvar
in loops. - Avoid deeply nested
if
statements when possible; use functions or early returns.
Control Flow MCQs
1. What is the primary purpose of an if-else statement in JavaScript?
2. What will be the output of the following code?
let x = 10;
if (x > 5) {
console.log('True condition');
} else {
console.log('False condition');
}
3. What does a switch statement evaluate?
4. What will be the output of the following code?
let choice = 'B';
switch (choice) {
case 'A':
console.log('You chose A');
break;
case 'B':
console.log('You chose B');
break;
default:
console.log('Invalid choice');
}
5. What is the ternary operator primarily used for?
6. What will be the output of the following code?
let age = 16;
if (age >= 18) {
console.log('Allowed');
} else {
console.log('Not allowed');
}
7. What is the purpose of the 'break' statement in a switch case?
8. What will be the output of the following code?
let age = 18;
let status = age >= 18 ? 'Adult' : 'Minor';
console.log(status);
9. What is the purpose of an else-if statement?
10. What will be the output of the following code?
let number = 2;
switch (number) {
case 1:
console.log('One');
break;
case 2:
console.log('Two');
break;
case 3:
console.log('Three');
break;
default:
console.log('Not a number');
}
11. How does the if-else statement work in JavaScript?
12. What will be the output of the following code?
let a = 5;
let b = 10;
if (a < b) {
console.log('A is less than B');
} else {
console.log('A is greater than or equal to B');
}
13. When is it preferable to use a switch statement over if-else statements?
14. What will be the output of the following code?
let isLoggedIn = true;
if (isLoggedIn) {
console.log('Access granted');
} else {
console.log('Access denied');
}
15. What happens if no case in a switch statement matches the input?
16. What will be the output of the following code?
let age = 15;
let status = (age < 18) ? 'Minor' : 'Adult';
console.log(status);
18. What is a common use of the ternary operator?
18. What will be the output of the following code?
let fruit = 'Banana';
switch (fruit) {
case 'Apple':
console.log('Apple is red');
break;
case 'Banana':
console.log('Banana is yellow');
break;
default:
console.log('Unknown fruit');
}
19. What type of expression can be used as a condition in an if statement?
20. What will be the output of the following code?
let num = 0;
if (num > 0) {
console.log('Number is positive');
} else if (num < 0) {
console.log('Number is negative');
} else {
console.log('Number is zero');
}