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!
Task Answer - Video Linkβ
πΉ 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