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 trialElena Paraschiv
9,938 PointsWhat is the purpose of function print(message)?
Can someone please explain this snippet of code and how is it used in the bigger picture. I dont figure out why is this added to the code?
function print(message) {
document.write(message);
}
2 Answers
Grace Kelly
33,990 PointsHi Elena, the purpose of this print function is that it removes the need to write document.write() everytime you want to output something to the screen, for example:
function print(message) {
document.write(message);
}
print("Hello, World!"); //outputs Hello, World! to the screen
var name = "Mike";
print(name); //outputs Mike to the screen
In your example above it is used in print(listHTML). This will output the contents of the listHTML variable to the screen.
Hope that helps!!
Elena Paraschiv
9,938 PointsThanks Grace!
William Ruiz
10,027 PointsBut document.write() isn't that long, and it is 1 line vs 3 lines. So is it intended to build a positive habit in newbies like me? In case in the future longer blocks can be called with a single function? I just don't know if I see its full efficiency at the moment because I know, say, Python just uses print() but ES6 doesn't have this recommendation?
Dan Nguyen
4,665 Pointsthanks!
Elena Paraschiv
9,938 PointsElena Paraschiv
9,938 Points