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 `do ... while` Loops

Kishan P
Kishan P
9,921 Points

I'm confuse

so why while loops is not perfect?

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Jake,

Just a tip that you're unlikely to get very helpful answers if you write a short question like this. Try to explain the problem you're having, what specific parts of a concept you do or don't understand, and you'll end up with better answers that will help you more.

Feel free to edit your question.

Kishan P
Kishan P
9,921 Points

Greg Kaleka I have been looking at this video for over and over again and i feel like watch it over 20 time helped me a lot and thanks.

Greg Kaleka
Greg Kaleka
39,021 Points

Cool! Re-watching videos can be really helpful. I also usually recommend practicing new concepts on your own with some slightly different inputs than what the teacher is using. Helps you go from memorization to real learning.

Kishan P
Kishan P
9,921 Points

any suggestion or website to practice these kinda concepts ?

Greg Kaleka
Greg Kaleka
39,021 Points

Anywhere you can run javascript.

For quick things, you can do it in your browser's console. For writing scripts to practice running, you can use Treehouse workspaces, or you can try websites like JS Bin.

2 Answers

Eric M
Eric M
11,545 Points

Hi Jake,

I'm not sure entirely what you're asking, so feel free to respond with more context for your question, but in case you're asking why we have both while and do while loops in JavaScript, it's because the while syntax won't run, ever, if its condition is false.

Do while allows us to do something once without checking any conditions, then check the condition to see if we should keep running the loop.

It's not about being perfect though! Most loops are very similar to eachother, because they all achieve a similar end - getting the same code to run over and over again (potentially with minor changes). You can do the same thing a for loop does in a while loop and vice versa.

In some cases you might have to really stretch the way the loop works to achieve it though. The different loop syntax is provided by the language for convinence.

Take a look at these three loops, they all do the same thing (well, one does something the others don't, see if you can spot what and why).

For loop

var basket = [];
var shopping_list = ["apple", "avocado", "milk", "coffee", "flour"];

// loop once for every item in shopping list
for (var i = 0; i < shopping_list.length; i++)
{
        // push the product at index i into our basket array
        basket.push(shopping_list[i]);
}

For in loop

var basket = [];
var shopping_list = ["apple", "avocado", "milk", "coffee", "flour"];

// loop through shopping list until we've operated on every item in the list
for (var product in shopping_list)
{
        // push the product from shopping list into our basket array
        basket.push(shopping_list[product]);
}

While loop

var basket = [];
var shopping_list = ["apple", "avocado", "milk", "coffee", "flour"];

// loop until our shopping list is empty
while (shopping_list.length > 0)
{
        // remove an item from shopping list and store in product
        product = shopping_list.pop();

        // store the product in our basket array
        basket.push(product);
}
Kishan P
Kishan P
9,921 Points

I still don't get it

Eric M
Eric M
11,545 Points

HI Jake,

What don't you get? Could you try to describe what you do understand regarding while loops and what parts you're not getting?