Skip to main content

Task 8: Understanding forEach and for Loops in JavaScript

Start with Affirmation​

  • "I am on the path to greatness. Every action I take leads me closer to my dreams. Challenges are opportunities, and I rise above them with confidence. My mind is clear, my heart is strong, and my journey is unstoppable."

Daily Affirmations for Success

  • βœ… I am capable, confident, and unstoppable.
  • βœ… Success flows to me because I take consistent action.
  • βœ… I attract opportunities that align with my purpose.
  • βœ… My hard work and persistence create abundance.
  • βœ… I am always learning, growing, and evolving.
  • βœ… I believe in my potential and trust my journey.
  • βœ… I turn setbacks into comebacks with resilience.
  • βœ… I am grateful for every success, big and small.

Success Reflection​

"Every step I take today strengthens my future success. I let go of doubt and embrace faith in my journey. I trust my abilities, I celebrate my progress, and I welcome even greater achievements ahead."

πŸ’ͺ Repeat daily, feel the power, and watch your success unfold!


πŸ”Ή If you don’t understand the task, watch the video above for the answer.


Task​


** Step 1: Why Do We Need Loops?​

"Imagine you have a list of itemsβ€”like a grocery listβ€”and you want to check each item one by one. Instead of writing separate code for each item, we use a loop to go through all items efficiently."

let groceries = ["Apples", "Bananas", "Carrots", "Milk"];

Step 2: Using the forEach Loop​

forEach is a built-in JavaScript method that loops through each item in an array and executes a function for each one. It’s great when you just want to perform an action on each element."

let groceries = ["Apples", "Bananas", "Carrots", "Milk"];

groceries.forEach(function(item) {
console.log("I need to buy: " + item);
});

Output :

  • I need to buy: Apples
  • I need to buy: Bananas
  • I need to buy: Carrots
  • I need to buy: Milk

Step 3: Using the for Loop​

let groceries = ["Apples", "Bananas", "Carrots", "Milk"];

for (let i = 0; i < groceries.length; i++) {
console.log("I need to buy: " + groceries[i]);
}

"In this loop, we manually define a counter (i), set a condition (i < groceries.length), and increase i in each step."

Step 4: Key Differences Between forEach and for Loops​

  • βœ… Use forEach when you simply want to process each item and don’t need to stop or skip any element.
  • βœ… Use for when you need full control over the iteration (e.g., skipping elements or stopping early).

Step 5: Using forEach with Index and Array Reference​

Did you know that forEach gives you access to the index and the whole array?

let groceries = ["Apples", "Bananas", "Carrots", "Milk"];

groceries.forEach(function(item, index, array) {
console.log(`Item ${index + 1}: ${item} (Total Items: ${array.length})`);
});


Recap – What Did We Learn?

Today, we learned:"

  • βœ… How to use forEach to loop through arrays easily
  • βœ… How to use for loops for more control
  • βœ… The differences between forEach and for loops
  • βœ… How to skip or stop loops using break and continue