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

Matthew Netherton
Matthew Netherton
2,819 Points

What does the follow code do exactly? What role does it play in the overall scheme of things?

Why do we include the following function?

function print(message) { document.write(message); }

I tried cutting it out, and when I did, the array didn't print to the website. But looking at it, I can't figure out what it does?

Anyone?

1 Answer

Christopher Gardner
Christopher Gardner
12,719 Points

This code would just print the passed message to the document.

function print(message) { document.write(message); }

print("Hello");
// prints Hello to the document

In the grander scheme of things, this could be useful if you just wanted to create a function to write things to the console for testing, and don't want to type "console.log" over and over.

function print(message) { console.log(message); }

print("Hello");
// prints Hello to the console

Hope this helps!