TGViewer
Web Development Web Development @webdevcoursefree · 79.7K subscribers
Post #3478 4.01K
Now, let's move to the next topic:

🔁 Javascript Conditions and Loops

🔹 Why Conditions Loops Matter
• They help your program make decisions
• They let your code repeat tasks
• Without them, JavaScript is useless for logic

Real use cases: Login validation, Form checks, Iterating data from APIs

🧠 Conditions (Decision Making)
Conditions run code only when a condition is true.

✅ if statement
let age = 20;
if (age >= 18) {
console.log("Eligible to vote");
}
• Code runs only if condition is true

 

🔁 if–else
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
• Two paths • One always runs

 

🔂 else if
let marks = 75;
if (marks >= 90) {
console.log("Grade A");
} else if (marks >= 70) {
console.log("Grade B");
} else {
console.log("Grade C");
}
• Multiple conditions checked in order

 

🔀 switch statement
Used when checking one value against many cases.
let day = 2;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
default:
console.log("Invalid day");
}
⚠️ Always use break to avoid fall-through.

 

🔁 Loops (Repetition)
Loops run code multiple times.

🔹 for loop
Best when number of iterations is known.
for (let i = 1; i <= 5; i++) {
console.log(i);
}
 

🔹 while loop
Runs while condition is true.
let i = 1;
while (i <= 3) {
console.log(i);
i++;
}
 

🔹 do–while loop
Runs at least once.
let i = 5;
do {
console.log(i);
i++;
} while (i < 3);
 

📦 Loop Control Keywords
• break → stops loop
• continue → skips iteration
for (let i = 1; i <= 5; i++) {
if (i === 3) continue;
console.log(i);
}
 

⚠️ Common Beginner Mistakes
• Infinite loops
• Missing break in switch
• Using == instead of ===
• Wrong loop condition

 

🧪 Mini Practice Task
• Check if a number is even or odd
• Print numbers from 1 to 10
• Print only even numbers
• Use switch to print day name

✅ Mini Practice Task – Solution 🔁

🟦 1️⃣ Check if a number is even or odd
let num = 7;
if (num % 2 === 0) {
console.log("Even number");
} else {
console.log("Odd number");
}

✅ Uses modulus operator • Remainder 0 → even • Otherwise → odd

 

🔢 2️⃣ Print numbers from 1 to 10
for (let i = 1; i <= 10; i++) {
console.log(i);
}
 

🔁 3️⃣ Print only even numbers from 1 to 10
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) {
console.log(i);
}
}
 

📅 4️⃣ Use switch to print day name
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
case 4:
console.log("Thursday");
break;
case 5:
console.log("Friday");
break;
default:
console.log("Invalid day");
}


➡️ Double Tap ♥️ For More
  • ❤ 14
  • 👍 1
More from @webdevcoursefree
  1. Oct 14, 2026🌐 Web Development Roadmap — Part 17 HTML Images — <img> Tag Images make webpages more vis…
  2. Oct 13, 2026𝗧𝗢𝗣 𝗙𝗥𝗘𝗘 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 𝗧𝗢 𝗕𝗢𝗢𝗦𝗧 𝗬𝗢𝗨𝗥 𝗦𝗞𝗜𝗟𝗟𝗦 𝗜𝗡 𝟮𝟬𝟮𝟲! Explor…
  3. Oct 12, 2026For example: Go to Profile → <a> Submit Form → <button> Open Article → <a> Delete Account…
  4. Oct 12, 2026🌐 Web Development Roadmap — Part 16 HTML Links — Tag Links are one of the most important…
  5. Oct 12, 2026🚀 𝗪𝗵𝗶𝗰𝗵 𝗧𝗲𝗰𝗵 𝗦𝗸𝗶𝗹𝗹 𝗪𝗶𝗹𝗹 𝗬𝗼𝘂 𝗠𝗮𝘀𝘁𝗲𝗿 𝗶𝗻 𝟮𝟬𝟮𝟲? Want to buil…
  6. Oct 9, 2026Post #4079
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →