Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!