FAQ: Loops - Nested Loops


This community-built FAQ covers the “Nested Loops” 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 Nested Loops

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!


2 posts were split to a new topic: Giving me an error


2 posts were merged into an existing topic: Giving me an error


Struggling to print this to the console as a String - it only prints numbers (1 and 2). Im not sure what I am doing wrong, any ideas? code below

const bobsFollowers = [‘john’, ‘sam’, ‘chris’,‘pen’]
const tinasFollowers = [‘john’, ‘michelle’, ‘sam’]
const mutualFollowers =
for (let i = 0; i < bobsFollowers.length; i++) {
for (let j = 0; j < tinasFollowers.length; j++) {
if (bobsFollowers[i] === tinasFollowers[j]) {


First of all, with mutualFollowers.push(i), you are pushing a number from the for loop into the empty array, not the actual value of one of the arrays. So it either needs to be mutualFollowers.push(tinasFollowers[j]) or mutualFollowers.push(bobsFollowers[i])

Next, if you console.log the push, you are actually returning the output of the .push method, which will always return the LENGTH of the array you are pushing into.

JavaScript Array push() Method
Return Value: A Number, representing the new length of the array

So when the nested loop gets a match, it will push the number (or the name, that doesn’t matter) in the array, and it will return the length of the array, which is now 1. Then it happens again at the second match and it will return the length 2.

var animals = ['pigs', 'goats', 'sheep'];

// expected output: 4


7 posts were split to a new topic: Nested loops


I just wanted a bit of confirmation. I have the correct code but just wanted to clarify something that didn’t make sense to me.

If I run the loop for either bobsFollowers or tinasFollowers the output is the number of items are in the array (i.e 4). But running the nested loop compares the names and produces the names that match.

I suppose what I’m asking is why isn’t it comparing number of items or lengths?

My code below:

// Write your code below
const bobsFollowers = ['Tasha','Jane','Mark','Steven'];
const tinasFollowers = ['Tasha','Ryan','Jane'];

const mutualFollowers = [];

for(let i = 0; i < bobsFollowers.length; i++){
  for(let j = 0; j < tinasFollowers.length; j++){
    if(bobsFollowers[i] === tinasFollowers[j]){

//output:  [ 'Tasha', 'Jane' ]


Because that is trivial information. All we want is the matches.