Welcome to our journey through JavaScript's for
loops - powerful tools for automating repeatable tasks. Using simple examples, we'll explore the For Loop, For-In Loop, and For-Of Loop. Let's get started!
Essentially, loops execute tasks repeatedly, akin to playing a favorite song on repeat. Here's a simple for
loop that prints numbers from 1 to 5:
JavaScript1for (let i = 1; i <= 5; i++) { 2 console.log(i); // Will print numbers from 1 to 5 3} 4/* 5Prints: 61 72 83 94 105 11*/
In this example, we initialize i
as 1
, set our condition as i <= 5
, and increment i
by 1
in each iteration with i++
. So, i
goes from 1
to 5
, and every time, we print the current value of i
to the console.
Note: i++
is an increment operation that increases the value of i
by 1. It basically the same as i = i + 1
or i += 1
, just in a shorter form.
Let's look at our example once again:
JavaScript1for (let i = 1; i <= 5; i++) { 2 console.log(i); // Will print numbers from 1 to 5 3}
The For Loop
comprises three elements:
- Initialization: Here,
i
starts at1
. - Condition: The loop persists as long as
i <= 5
. - Changes: Here,
i++
increasesi
by1
each loop iteration.- Changes can be anything, we could also decrease
i
by 1 every time if that would make more sense.
- Changes can be anything, we could also decrease
The structure of the loop is always as follows:
JavaScript1for ([initialization]; [condition]; [changes]) { 2 [loop body] 3}
Consider, for instance, how we might list all seven days of the week:
JavaScript1let days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; 2for (let i = 0; i < days.length; i++) { // 'i' goes through all 'days' list indices 3 console.log(days[i]); // Prints each day of the week onto the console 4} 5/* 6Prints: 7Monday 8Tuesday 9Wednesday 10Thursday 11Friday 12Saturday 13Sunday 14*/
A for-in
loop iterates over properties of an object, similar to checking items in a backpack:
JavaScript1let star = { 2 name: "Sun", 3 color: "Yellow", 4 size: "Giant", 5}; 6for (let property in star) { 7 console.log(property, '->', star[property]); 8} 9/* 10Prints: 11name -> Sun 12color -> Yellow 13size -> Giant 14*/
In this example, property
steps through each property in the star
object, and star[property]
retrieves the value for each property.
The for-of
loop processes iterable objects like arrays or lists. Imagine a daily task list:
JavaScript1let planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']; 2for (let planet of planets) { // iterate through all planets in the list 3 console.log(planet); // Prints every planet onto the console 4} 5/* 6Prints: 7Mercury 8Venus 9Earth 10Mars 11Jupiter 12Saturn 13Uranus 14Neptune 15*/
Here, planet
traverses all the elements in planets
, printing each one. Note that when using for-of
, you don't need an index in the list - you iterate through the elements of the list themselves.
Excellent work! You have now mastered JavaScript for
, for-in
, and for-of
loops. These integral building blocks will enable you to write clean and efficient code.
Coming up next, you will sharpen these skills in practical exercises. Practice is crucial to reinforcing the knowledge you've acquired. Best of luck!