For In Loop

3 minutes
Share the link to this page
Copied
  Completed
The for...in loop is used to loop through an object's properties. As we have not discussed Objects yet, you may not feel comfortable with this loop. But once you understand how objects behave in JavaScript, you will find this loop very useful.

Transcript

Hello in this JavaScript programming video, I am going to show you the for in loop. So if you haven't checked out the for loop video, I highly recommend that you take a look at that and then come back here. So welcome back if you took a break to have a look at the for loop video, okay, so the for in loop pretty similar, but it is useful if you have an object like this, for example, and you want to be able to loop over it. Normally, if you want to access items with this object to make an object number a separate section for objects covering all of this and you know, several other different types of objects. Feel free to check that out. And to work with it.

We'll do something like a where A is the key one is a value. Yeah, one for do. See for example, I get free. Okay, that's, you know, pretty simple, but imagine if there were You know, 100 of the the in here, you don't know what the keys are necessarily. This is where a four in loop really comes into play. And you could do whatever you want with it, you could loop over some data from the database, maybe it's something you've got from a database, whatever, I'm simply going to print it out to the console, say put the keyword store, open close bracket.

The difference here, enter the pre free parts and you would with a normal for loop, you've got VA and a call list. Item is in need an object. Okay, so what this is essentially doing is getting every single item one by one from this object. So it's the same for every item. This is what we're creating in amazing object. Now, console dot log.

All we have to do is to amazing mayson object item, save that reload. And you can see it prints it all out. Let's have a look at what what what what automatically it will print out item will go also do a colon between it so you can see the difference. The item is simply the key, he just gets the key. So if I change this to my name, so for example, reload, you know, this is dynamic and it update it doesn't matter how long the subject it doesn't matter the names of the keys of the object. It just works.

So that's it for this video. If you have any questions, feel free to pop me a message. Feel free to take a look at the GitHub link as well. And as usual, I look forward to seeing you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.