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

brandonlind2
brandonlind2
7,823 Points

Please explain message and the array function

I would greatly appreciate it if someone could explained the second half of this video. There's a few things that I don't understand. First is the function print(message) {document.write(message)},what is the message? Is it a variable or array, also what does it do in this example, the thing that confuses me the most is that it looks undefined, what data does it hold and how does it hold data? The second thing is that he said function printlist(list) {} has a parameter that's an array.This looks the same as a variable parameter how do you distinguish between a variable and an array parameter? Does javascript only treat parameter variables and arrays differently when you call them?

4 Answers

Hey Brandon,

function print(message) {
    document.write(message)
}
  • "What is the message?"
    • It's called a parameter. Functions have the ability to take input and then do something with that input. In order to tell the JavaScript interpreter that a particular function can take a parameter ... and also in order to keep track of that parameter while inside of the function ... we give that parameter a name — a handle. In this case that name was message... but it could have been anything... it could have been chickenNugget.
function printAgain(chickenNugget) {
    document.write(chickenNugget) //this does the same thing as that print() function above
}
  • Is it a variable or array, also what does it do in this example, the thing that confuses me the most is that it looks undefined, what data does it hold and how does it hold data? The second thing is that he said function printlist(list) {} has a parameter that's an array.This looks the same as a variable parameter how do you distinguish between a variable and an array parameter? Does javascript only treat parameter variables and arrays differently when you call them?
    • So when you call the function, you can supply an input. At that point, the thing we've been calling a parameter this whole time is now called an argument. That argument can be of any data type: array, object, string, number etc. But, of course, not every data type is going to be a welcome argument in every function. The person who writes the function gets to decide what all the possible arguments to a function can be.
function print(message) { //"message" here is simply a parameter (read: placeholder)
    document.write(message); //This function only works as intended if you give it an argument with a data type of String
}

function printList(list) {//"list" here is simply a parameter (read: placeholder)
    for(var i=0; i < list.length; i++) {
        document.write(list[i]); //This function only works as intended if you give it an argument with a data type of Array
    }
}

//Below, the above functions are called... their PARAMETERS are replaced with appropriate ARGUMENTS

print("Respect Mah Authoritay!"); //This writes to the Document: "Respect Mah Authoritay!"

printList(["Respect Mah Authoritay!", "Please", "Kind Sir"]);  //This writes to the Document: "Respect Mah Authoritay!", then "Please", then "Kind Sir"

Notice that the print() function only makes sense if you supply a String as an argument and that the printList() function only makes sense if you supply an Array as an argument.

Julian Aramburu
Julian Aramburu
11,368 Points

Great answer Jacob!

Also , the second function, "printList" is a function prepared specially to print arrays that's why Dave included a for loop into the function, in order to iterate each element of the array and print it in a List format.

If you are having difficulty understanding how the print function is executed, I would suggest going back and reviewing the JS basics course on functions, Dave does a great job explaining it.

Message in this case is a parameter. it is a place holder for an argument when the function is called. Message can be whatever you want it to be, you can pass an array, a variable, whatever.

Dave does a much better job at explaining this than I, but It is undefined until you pass an argument into the function. The function printList(list), is the same format as print(message). There is no difference between parameters, the only thing that is different is what argument you pass into the function.

I hope this helps.

function print (message) is how he is printing the playlist to the document. After the for loop in the printSongs function he closes the ordered list then calls the print function and passes the listHTML variable to it, printing the playlist to the document. Message is simply a parameter that he sets for the print function. One way to test this is by simply commenting out the print function and seeing how it alters the result of the code when launched in a browser.

For the second part it really doesn't matter when distinguishing between the two. We know it is going to be an array because that is what he is looping through, and declares the playlist variable above as an array. If it helps you could change the name from list to something like listArray so you know that you are going to be passing an array into the function when you call it.