Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
210,487 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
210,487 Points

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