DEV Community

Srivastan
Srivastan

Posted on

for in && for of Loops

So everyone is found of for loops , so the idea of Publishing this article is to make clear my understanding on for in && for of loops,
let us begin the story , When I was going through my basics on JavaScript found an interesting concept called as For in Loop …hmm interesting why it is interesting ?? does it make magic , Yeah it actually, Does consider an Object in an world like an car what

var Car = {
Engine Type: Twin Cylinders ,
Speaker System: Ac,
Color: Red

}
so if you want to by an new Car and if you approach an Sales Robot and ask him what are the Properties of the Car is , and the developer Behind the Robot is Going to help him through , guess what an For in loop , So Wtf are you trying to say Lets apply the loop to the Object ,

Var Car = {
EngineType: Twin Cylinders ,
SpeakerSystem: Ac,
Color: Red

}

  for (const property in Car) {
      console.log(property);
     }
Enter fullscreen mode Exit fullscreen mode

OutPut:

"EngineType"
"SpeakerSystem"
"Color"

So What about an For in .. in an Array , Hmm... That's an 'A' Grader's Question , So developer takes it as Challenge and Code them

Consider an Array which a Mom gives you for items in an Fridge,
Var Fridge = ['Eggs', 'Cola', 'Abounded Cheese' ,'Chocolate'];

So if  Developer Applies an For in.. Loop on the array,
      for (const property in Fridge) {
              console.log(property);
          }
Enter fullscreen mode Exit fullscreen mode

OutPut :

"0"
"1"
"2"
"3"

So its going to Give the Indexes of the items inside the Array fine, So the Title consists of Two topics , So yeah u guys Guessed it its just Lead for the Second one ('Weird Smile on My Face')...
So to find the items , We can use For of Loop....

   Lets Loop the question..('Looping is nothing but Copying and Pasting the Same Stuff...#Philoshper')
Enter fullscreen mode Exit fullscreen mode

Consider an Array which a Mom gives you for items in an Fridge,
Var Fridge = ['Eggs', 'Cola', 'Abounded Cheese' ,'Chocolate'];

So if  Developer Applies an For of.. Loop on the array,
      for (const property of Fridge) {
              console.log(property);
          }
Enter fullscreen mode Exit fullscreen mode

OutPut :

"Eggs"
"Cola"
"Abounded Cheese"
"Chocolate"

Basically the Moral of Story is just an Simple grammar
'in' Stands for indicate 'inclusion, location, or position' and
'of' Stands for 'expressing the relationship between a scale or
measure and a value'.

So Basically What will Happen When the loops used for Array of Objects ??

Comment it Out ….

Top comments (0)