Skip to main content

Featured

🔠 Day 8 — Strings & String Methods in JavaScript

🔠 Day 8 — Strings & String Methods in JavaScript Welcome to Day 8 🌟 — Today’s session is all about **Strings**, one of the most common data types in JavaScript. Strings represent text and allow you to store names, messages, or any sequence of characters. 💬 1️⃣ What is a String? A string is a sequence of characters written inside quotes. let name = "Rahul"; let greeting = 'Hello World'; let message = `Welcome ${name}!`; // Template literal 🧩 2️⃣ String Properties Strings behave like arrays — they have a .length property and can be accessed using index numbers. let text = "JavaScript"; console.log(text.length); // 10 console.log(text[0]); // J console.log(text[text.length - 1]); // t 🎨 3️⃣ Common String Methods toUpperCase() & toLowerCase() let city = "Hyderabad"; console.log(city.toUpperCase()); console.log(city.toLowerCase()); slice(start, end) let word = "JavaScript"; console.log(word.slice(0, 4)...

🔂 Day 5 — Loops in JavaScript

🔂 Day 5 — Loops in JavaScript

Welcome to Day 5 🎉 — you’ve now learned how to make your program think with conditions, but what if you want to repeat a task **multiple times** automatically? That’s where **loops** come in! Loops let you run the same block of code until a condition is met — saving time, code, and effort.

🧩 1️⃣ What Are Loops?

A loop runs a section of code again and again until a certain condition becomes false. They help automate repetitive operations such as printing numbers, iterating arrays, or validating user input.

🔁 2️⃣ Types of Loops in JavaScript

1️⃣ for Loop

Used when you know how many times you want to run a block of code.

for (let i = 1; i <= 5; i++) {
  console.log("Iteration:", i);
}

2️⃣ while Loop

Runs as long as the given condition is true.

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

3️⃣ do...while Loop

Similar to while, but runs the code block **at least once**, even if the condition is false initially.

let num = 1;
do {
  console.log("Number:", num);
  num++;
} while (num <= 3);

🎯 3️⃣ Loop Control Statements

break

Stops the loop entirely.

for (let i = 1; i <= 5; i++) {
  if (i === 3) break;
  console.log(i);
}

continue

Skips the current iteration and continues the next one.

for (let i = 1; i <= 5; i++) {
  if (i === 3) continue;
  console.log(i);
}

🔢 4️⃣ Nested Loops

A loop inside another loop — useful for grids or patterns.

for (let i = 1; i <= 3; i++) {
  for (let j = 1; j <= 3; j++) {
    console.log(`i=${i}, j=${j}`);
  }
}

🧮 5️⃣ Real Example: Sum of Numbers

let sum = 0;
for (let i = 1; i <= 5; i++) {
  sum += i;
}
console.log("Total Sum:", sum);

🧠 6️⃣ Practice Task

  1. Print numbers from 1 to 10 using a for loop.
  2. Display even numbers up to 20 using while loop.
  3. Use do...while to print numbers from 5 to 1.
  4. Try using break to stop when the number reaches 7.

🎯 Summary

Loops are powerful for automation. Tomorrow, we’ll introduce **Functions and Scope**, where you’ll learn to organize your logic and reuse code efficiently.

Comments