💻 JavaScript Roadmap 2026 — Part 4
🟢 Conditional Statements & Loops
Programs often need to make decisions and repeat tasks.
For example:
• If a user is logged in → show the dashboard.
• If the payment fails → show an error.
• Repeat a task for every item in a list.
• Continue processing until a condition becomes false.
JavaScript provides conditional statements and loops for this.
1️⃣ "if" Statement
The "if" statement executes code only when a condition is "true".
let age = 20;
if (age >= 18) {
console.log("You are an adult");
}
Since "age >= 18" is "true", the message is printed.
Basic structure
if (condition) {
// code to execute
}
2️⃣ "if...else"
Use "else" when you want to execute another block if the condition is false.
let age = 16;
if (age >= 18) {
console.log("You can vote");
} else {
console.log("You cannot vote");
}
Output: You cannot vote
3️⃣ "else if"
Sometimes there are multiple conditions.
let marks = 75;
if (marks >= 90) {
console.log("Grade A+");
} else if (marks >= 75) {
console.log("Grade A");
} else if (marks >= 60) {
console.log("Grade B");
} else {
console.log("Grade C");
}
Output: Grade A
JavaScript checks the conditions from top to bottom. Once it finds a true condition, the remaining "else if" blocks are skipped.
4️⃣ Multiple Conditions
You can combine conditions using logical operators.
let age = 25;
let hasID = true;
if (age >= 18 && hasID) {
console.log("Access granted");
}
Both conditions must be true because we used "&&".
Using OR
let hasEmail = false;
let hasPhone = true;
if (hasEmail || hasPhone) {
console.log("Contact information available");
}
At least one condition needs to be true.
5️⃣ Nested "if"
An "if" statement can exist inside another "if".
let isLoggedIn = true;
let isAdmin = true;
if (isLoggedIn) {
if (isAdmin) {
console.log("Admin dashboard");
}
}
Nested conditions can be useful, but too much nesting can make code difficult to read. When logic becomes complicated, consider restructuring the conditions.
🔹 "switch" Statement
6️⃣ What is "switch"?
"switch" is useful when you want to compare one value against several possible values.
let day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the week");
break;
case "Friday":
console.log("Almost weekend");
break;
default:
console.log("Regular day");
}
Output: Start of the week
7️⃣ Why "break" Matters
let number = 2;
switch (number) {
case 1:
console.log("One");
break;
case 2:
console.log("Two");
break;
case 3:
console.log("Three");
break;
}
"break" tells JavaScript to exit the "switch". Without it, execution can continue into subsequent cases. This behavior is called fall-through.
8️⃣ "default"
"default" runs when none of the cases match.
let fruit = "Mango";
switch (fruit) {
case "Apple":
console.log("Apple");
break;
case "Banana":
console.log("Banana");
break;
default:
console.log("Unknown fruit");
}
Output: Unknown fruit
🔁 LOOPS
Loops allow you to execute code repeatedly.
Instead of writing:
console.log(1);
console.log(2);
console.log(3);
you can use a loop.
9️⃣ "for" Loop
The traditional "for" loop has three parts:
for (initialization; condition; update) {
// code
}
Example:
for (let i = 1; i <= 5; i++) {
console.log(i);
}
Output: 1 2 3 4 5
Let's break it down:
• let i = 1 → Start with 1.
• i <= 5 → Continue while "i" is less than or equal to 5.
• i++ → Increase "i" after every iteration.
🔟 Understanding an Iteration
For:
for (let i = 1; i <= 3; i++) {
console.log(i);
}
JavaScript roughly follows:
• i = 1 → condition true → print 1
• i = 2 → condition true → print 2
• i = 3 → condition true → print 3
• i = 4 → condition false → stop
Understanding this process is more important than memorizing the syntax.
Post #3697
1.4K
- ❤ 1