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 trialZachary Smith
Full Stack JavaScript Techdegree Graduate 16,616 PointsI 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
231,236 PointsThis 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").
Shung Chen
6,526 Pointsfunction 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
Zachary Smith
Full Stack JavaScript Techdegree Graduate 16,616 PointsThank 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.
Zachary Smith
Full Stack JavaScript Techdegree Graduate 16,616 PointsOkay, 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
231,236 PointsDon't worry, you'll get plenty more practice with functions as you continue on.