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

I don't understand how the 'message' and 'list' parameters work

How does the function know what to put in for the value of the those parameters? Like 'message' isn't defined anywhere so how does it know what to display in document.write(message)? I'm just as confused with the 'list' parameter because I don't understand how it knows how long list.length is when it's not defined.

2 Answers

Yacine Diop
Yacine Diop
3,465 Points

Think of this way:

  1. while writing the function, list is added as a parameter (I think of this as a placeholder); however, the intention for that space is for playlist later on.
  2. when we call the function to print, we now can place the var playList. This is our argument from the get go.

So while making the function, don't think list is not defined, instead the list will be defined later.

Same thing for document.write(message). Message is a placeholder, a parameter set into function print(message). We later called the function print(listHTML);

listHTML replaces the placeholder message.

This is how I think about it to make it clear to myself. Hope it helps someone.

Steven Parker
Steven Parker
229,732 Points

Parameters act as placeholders for the actual value that will be passed to the function when it is called.

You might think of a parameter as a variable that is declared by naming it in the function definition, and is automatically assigned when the function is called, getting set to the value supplied as the argument.

If a parameter is a list, the length of the list will be determined when the function is called and the real list is supplied as the argument.