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 Tracking Multiple Items with Arrays Using For Loops with Arrays

this code was all understood except for how does the function printList(list) know that it has to 'look'

at var playList array for the looping . dont get how (list) is related to function playList?

4 Answers

Dennis Brown
Dennis Brown
28,742 Points

The key thing to remember is that when the function is written out, it's a blueprint for the function's actions when it's called. When the function expects parameters to come in as arguments, it maps those arguments out with their own specific names, "whatever comes in as this argument list, we are going to always refer to it as "list", and nothing else!". In other words throughout the rest of the function the parameter passed in is seen as "list", regardless of the name of the array or object passed to that argument. If we were to change the argument name in the function, it would still work the same, except we would have to change all instances of "list" to match the new argument name.

Here's an example:

// we change the argument name to 'newList'
function printList( newList ) {
  var listHTML = '<ol>';

  // since the argument has changed, the function doesn't know what 'list'
  // is, so we change all references to 'newList'. This is the new _nickname_  for the array
  for ( var i = 0; i < newList.length; i += 1) {
    listHTML += '<li>' + newList[i] + '</li>';
  }
  listHTML += '</ol>';
  print(listHTML);
}

// when we call it, everything works as intended
printList(playList);

// we can give it another playlist and it works the same way
printList(secondPlayList);

So when we use the printList function, no matter what array, or object is passed to it, the function will always see it as "list", or the argument name given. It's basically the reference of that array in it's lexical space (function space). It is all internal to the function, and those arguments are specific to the function's use, and reference.

If you are only changing the argument name, and it still works, make sure to refresh the instance, as it's probably still using the old one.

I hope that makes more sense now. :D

Sergiy Khaman
Sergiy Khaman
2,989 Points

Brilliant explanations! I've got lost initially myself, paying attention to Exact details of names and arguments, loosing bigger picture. Dave McFarland should give more time for that part of "list" in his video. Thank you.

Dennis Brown
Dennis Brown
28,742 Points

It's not very apparent, as there should be an empty line after the function, but the last line typed after the function is the function being called with the array name.

printlist(playList);

So by doing so, you are calling the function with the array as the parameter for the argument.

Also keep in mind that the argument used for a function can be anything, as long as it is referenced as the same name in the function itself; it does not need to match the parameter given to the function to work.

I hope that helps! If not, I'll be happy to revise the answer. :)

Amazing answer. Can't thank you enough.

I get that playList array is passed as an argument to the function. My question is , how does (list) and (playList) behave the same. I tried replacing( list) parameter with (playList) parameter and it workrd. just not clear as to how passing a parameter of (list) to the printList function makes the code take values from the playList array

Function parameter is just a name (like a name of a variable for example) - so at this point it didn't matter much how you name the parameter. And parameters can take any argument, as mentioned by Dennis.

Matthias Nรถrr
Matthias Nรถrr
8,614 Points

This is also my question. Why did the Tutor call the array inside the function "list" and not "playlist" ? We already created the array "playlist" so it would make sense, to again call the array inside our new function also "playlist".

To me, giving the two arrays different names, was confusing.

thank you . I think I am starting to understand:)

Dennis Brown
Dennis Brown
28,742 Points

Great to hear!

Feel free to ask away if there is anything that still doesn't make sense. :)