Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Exiting Loops

Christopher Mlalazi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 Points

How Will I Remember All These Loops?

I have a very simple question. How am I supposed to remember all these loops that we have just learned? Maybe its the examples used, these guessing games, but right now they seem just too much for me to grasp properly.

8 Answers

A good way to remember what type of loop you should use is to think about what you want your program/loop to do. Let's say you want to loop over something multiple times but are not sure how many times exactly, A while loop, and a do while loop does that exact thing. It loops until the is condition is false. the only difference between a do while and a while loop is, a do will execute the contents inside the loop once before the condition statement is evaluated.

Let's say you have an array, and you want to loop through it. This is where the for loop really comes in handy. You set the condition with an array is by using <, >, ect. plus .length. The loop will iterate thought the length of the array doing whatever code you want it to do.

And the for in loop. this is used when looping through properties in objects.

there are several other loops out there, but these 4 are a great base. the best way to remember what loop to use is first ask yourself why and what and I doing with the loop, and also practice, CODE EVERY DAY!.

So the general rule of thumb is, use while/do while loops, if you don't know how many times you need to iterate until a condition is false. A for loop is used for arrays, and a for in loop is used on properties in objects.

I hope this helps.

It will take time to remember everything. The best way is to practice everyday by coding. When your watching these videos for the first time it is most important to understand the concepts and the logic behind them. Most programmers don't memorize every little thing but they understand how the different loops and arrays work so they know when to use them but they may need to reference the proper syntax in the online documentation and there is nothing wrong with that. Good luck with your studies!

Jesus Mendoza
Jesus Mendoza
23,288 Points

That's why you study and practice, to learn and remember things, if you don't practice you forget things.

Christopher Mlalazi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 Points

This is the second time I am going through the loops, and everytime I can't just seem to remember which type of loop should be used to a particular situation. I guess with time it will come but for now whew!

Jesus Mendoza
Jesus Mendoza
23,288 Points

It's ok bro we all feel that way when we are learning, with the time and experience you will know what to use and when to use it

Just practicing for me isn't enough if I don't understand the logic behind why we use different loops. Like I totally get that the do/while and while loops are similar except the do/while executes once...however, Dave never compared the do/while to the for, which I wish he would've...so I'm left wondering about the similarities/differences/when is best to use one vs. the other between those 2. No amount of practice will correct not understanding this, I have to look for answers first.

Christopher Mlalazi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 Points

I am still a long way from understanding how JavaScript works, but now I have a good idea. Things like loops, functions, variables are taught as separate entities, but when one gets down to writing functional JavaScript, these entities are all grouped and interconnections made between the groups so that they produce an a single working output for the user, just like team work.

Mr Sri
Mr Sri
9,200 Points

If you are using an text editor, A good idea would be to save important blocks of code as snippets (clips in coda).

Christopher Mlalazi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 Points

Thanks Micah! Now I know I should not try to cram everything but just get a general idea how it works and of course there will be the online libraries for references. Now my anxiety has dissipated. :)

kevinardo
seal-mask
.a{fill-rule:evenodd;}techdegree
kevinardo
Treehouse Project Reviewer

I have used flash cards to brain wash myself :) Have a look at this method mate, i can highly recommend it:

http://www.jackkinsella.ie/2011/12/05/janki-method.html

Good Luck with your studies, and NEVER GIVE UP :)

Alfredo Gayou
Alfredo Gayou
7,051 Points

also almost all IDE have a way to help you with snippets, and as jacob said for is mainly used for arrays and for other uses they can be used indistinctively