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 Two-Dimensional Arrays

I don't understand how the print function works in this program

How does the following code"

function print(message) { document.write(message); }

know to print the above playlist? I don't get it.

3 Answers

Steven Parker
Steven Parker
231,268 Points

This code just defines the function named "print", it doesn't cause it to run.

Elsewhere in the code, the function will be invoked (or "called"), which makes it run. In the video, this happens inside the "printSongs" function where it is given the name of the thing to output ("listHTML").

function print(message) { document.write(message); }

the setup of function print() is simply helping people to shorten the code needs to type after you define the function print()

whenever you need to type

document.write(your content here) is shortened to print(your content here)

You don't need it but it's shortened - although it does not make much difference for me

Thank you for taking the time to answer, Shung Chen.. Over the past week, I've understood this concept a lot better since I used it to make a project.

Okay, thank you for taking the time to answer. I still don't completely get but for now I'll take your word for it.

Steven Parker
Steven Parker
231,268 Points

Don't worry, you'll get plenty more practice with functions as you continue on. :wink: