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)

Tomas Svojanovsky
Tomas Svojanovsky
24,500 Points

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.