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

sebbe12
sebbe12
4,015 Points

function print(message) { document.write(message); } I don't really understand

What does this function do exactly. It seems like it hold the texts/info from function printList but how.

Without it everything is gone. In every other exercise we have done in the course from the beginning til now only used document.write() to get something on the page why do we have to do a function now?

2 Answers

It's not uncommon to see people write functions (often called 'abstractions') that make it simpler to call other things.

So in this case I imagine they do this because 'print' is shorter than 'document.write()'. Also 'Window.print()' is a built in function in Javascript so they may building up a mental reference for the students to the print function. (https://developer.mozilla.org/en-US/docs/Web/API/Window/print)

Document is an object which holds some properties (f.e. document.images) and functions. In your example it is this - https://developer.mozilla.org/en-US/docs/Web/API/Document/write.

Function print take a parameter message and put it into document.write. It means - take the variable and write it into document. You can try it in the console of your web browser. It deletes all the content and puts it between body tag.