FAQ: Loops - Looping through Arrays

This community-built FAQ covers the “Looping through Arrays” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Looping through Arrays

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

const animals = [‘Grizzly Bear’, ‘Sloth’, ‘Sea Lion’];
for (let i = 0; i < animals.length; i++) {

I’m having a conceptual problem with how this can be the stopping condition:
i < animals.length

So, assuming:
[‘index 0’, ‘index 1’, ‘index 2’]
[‘Grizzly Bear’, ‘Sloth’, ‘Sea Lion’]
then it seems to me that the code block should stop when i (index value) is less than animals.length (3), which always has to be true, because beyond i=2, there are no values for i.

What am I seeing incorrectly?

When it fails, the loop stops.

stopping condition is not really the term to describe the conditional expression here, but so be it if that is how the teacher wishes to describe it being that it is a conditional that effects termination of the loop. Perhaps stopping conditional would be more apt so we don’t read in that it is the condition that will stop the loop. Quite the opposite, it is the condition that allows the loop to continue.

As it is written in the code, we could see it as the looping condition which is dependent upon ‘true’ to keep going.


MTF’s thought to see it as a
“continuing condition”, rather than a “stopping condition”, helped me out a little, and I think I get it now. i is not equivalent to a value in the code block, rather to an index position. So it’s better to think of i as a generated number (starting off at ‘0’, the iterator) which will serve as index positions in the code block.

If i=0, the operation will perform, since it’s less than 3. (and then the the index position of ‘0’ will be put into the code block, which will then log to the console whatever is in that index position in the array: i.e., ‘Grizzly Bear’.)
If i=1, the operation will perform, since it’s less than 3. (and then the the index position of '1" will be put into the code blcok, and the console will log whatever is in that index position in the array: ‘Sloth’.)
If i=2, the operation will perform, since it’s less than 3. (and then the the index position of ‘2’ will be put into the code blcok, which will then pointt to what’s in that index position in the array: ‘Sea Lion’.)
if i=3, the length of the array,the code block will not run, and the loop is terminated.

Ugh, not sure if that helped. May have caused more confusion! But I think the word “value” points to where your issue is.



Thanks for the explanations and step-by-step walk-through! I understand it much better now. My main misunderstanding I think was assuming the stopping condition (more aptly referred to as the looping/continuing condition) stopped the block from running if it outputted TRUE but, if I understand correctly, an output of TRUE continues the block, whereas FALSE stops it from running.

1 Like

Would this loop not work if the array looked like this

vacationSpots[‘a’, ‘b’, ‘c’];

also where it says ’ i < vacationSpots.length’ which vacationSpots.length does it check itself with ?

edit: I made a mistake I thought vacationSpots.length checked the length of the words in the array rather than the amount of values in that array

1 Like

The length is the count of elements in the array, just for clarification.

I used the following code originally and got the right resut, but was marked wrong.

const vacationSpots = [‘Bali’, ‘Paris’, ‘Tulum’];

// Write your code below
for( i = 0; i < vacationSpots.length; i++){
  console.log("I would love to visit " + vacationSpots[i]);

Afterwards I added the “let”, and my code didn’t change, but I was allowed to pass. What was the difference between my code with and without the let

let gives variables block scope which keeps them from leaking into their parent scope, or worse into global scope. It’s actually a very important concept in JS.


when i did

for(let  i = 0;  i = vacationSpots.length;  i++) {

I got a really long list of the vacation spots. but according to the calculations above, shouldn’t i have gotten only one extra line of vacationSpots?

That should be a relation check, not an assignment.

i < vacationSpots.length;

if there are 3 vacationSpots, and with index value its 2 ( because you count from 0), then .length should give me ‘one’ more. no? (till 3)

ok, i get it, this would be true if it would have been a stop condition. it would go on, till it saw it did it already 3 times. but since this is not a ‘stop condition’ , but rather a looping condition, it will go on and on, because it will always answer true

1 Like

Can’t I use var instead of let?

for (var i = 0; i < vacationSpots.length; i++) {
  console.log(`I would love to visit ${vacationSpots[i]}`);

It just keep asking me to use let! So what is the problem of using var?

It’s not a problem, per se, only that it is a legacy keyword that has gone out of fashion in ES6. let is intended to overcome a weakness that var presented by introducing true block scope. The author is likely hoping to help learners get away from using it. let is the more preferred keyword, these days and moving forward.

I keep getting an error when trying to run this exercise. The only way I could proceed was by getting the solution, which to me looks exactly like my code. Is it a bug?

for (let i = 0; i < vacationSpots.length; i++) {
  console.log('I would love to visit ' + vacationSpots[i]);

Even when getting the solution, copying the result, resetting the exercise and trying to run the solution, I get an error.

Great answer Nick! Good job sir! That was very helpful and through answer.