for, forEach , for of and for in loops in JavaScript

for, forEach , for of and for in loops in JavaScript

·

3 min read

You probably know the for and forEach loop but you may not know about for of and for in loops🤔. for of and for in are actually new in JavaScript and their usage is not so frequent yet.
Let’s talk about all of these four loops one by one...

for loop

Let’s make a simple array of fruits.

fruitsArray = ["apples", "bananas", "oranges", "peaches", "berries"]

We have made an array called fruitsArray having five items, right? Let’s print out all items through for loop.

for(let i = 0; i < fruitsArray.length; i++){ 
console.log(fruitsArray[i]); 
}

Above for loop give us the following output. (checked for chrome console)

apples
bananas 
oranges 
peaches 
berries

forEach loop

In our for loop, we got all the items(that were fruits) one by one. Now let’s head over to forEach loop to know how it is handling things.

fruitsArray.forEach(item => { 
console.log(item); 
})

Here is the output(same as our previous for loop)

apples 
bananas 
oranges 
peaches 
berries

Now let’s talk about the new loops (I guess you’re waiting for this!).

for of loop

for(item of fruitsArray){ 
console.log(item);
}

On first iteration, this loop is saying, hey give me first item of fruitsArray then after first iteration give me second item and so on this iteration process continues until last item of fruitsArray.
It has same output as for previous for and forEach loop.

for in loop

for in loop is such loop that is not for arrays, it is for objects. Actually, it is used to print out the properties of objects.
Let’s take an example:

const fruitsQuantity = { 
apples:5, 
oranges:10, 
grapes:20 
}

We have an object called fruitsQuantity that has properties as different fruit names. Along with every property, there is a corresponding number that shows the quantity of each property.
Now let’s print out all properties of this object.

for(items in fruitsQuantity){ 
console.log(items); 
}

This loop give us all properties of fruitsQuantity object. See output below.

apples 
oranges 
grapes

for in loop has not kind of iterating process. Because we never heard iteration on objects, we heard iteration on arrays or strings. For me, it seems okay for understanding.
After all this discussion, we were just talking about how these loops were working. Now lets talk about some differences.
for loop takes more time and space while writing code and its Syntax is confusing if we have many lines of code.
for each loop seems okay to write while we are saying that hey give me all items or indexes on each iteration. This seems nice and close to human understanding.
for of loop is actually better than for each loop while tracing the code for understanding, it is just saying hey give me items of an array one by one. And also it takes less space while writing. A beginner or novice doesn’t feel confused while reading for of loop.
for in loop working is similar to for of loop. The difference is, for of loop is used for arrays and strings whereas for in loop is used for objects. for in again is simplified version to comprehend the code.

Conclusion

See, the Conclusion here is that with the passage of time languages syntax is becoming user-friendly so that when a person writes or traces the code he or she can easily comprehend it.

Note: This is my first post at Hash Node. If there is any mistake, let me know.

Always Learn Right and Carry on the Learning Process!🙂🥦💻😇

###

Â