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 Build a Quiz Challenge, Part 1 Solution

i cant understand what this code do function print(message){ document.write(message) }

i wont some one explane function print(message){ document.write(message) }

1 Answer

Catherine Gracey
Catherine Gracey
11,521 Points

This is the outer code: function print(message){}

The first word, function, tells the interpreter that you are creating a new function. The second word, print, tells the interpreter that the new function will be called print. The third section (message), tells the interpreter that the new function will take one parameter. That basically means you are creating a local variable called message. When you run this program, whatever you put between the () will be the message. The next section is marked by {}. This is the code that runs when the function is called.

This is the inside code: document.write(message) The first word, document, is talking about the HTML document that is in the browser. The second bit, .write is a function that is being run on the document. It's pretty straight forward - it writes to the document. The third bit, (message) is what is being written to the document. It's the contents of the variable you created in the first section.

To use this code, you could have something like the following:

var yourMessage = "JavaScript functions can be hard to understand at first.";
print(yourMessage);

The interpreter will then assign the value of the variable yourMessage to the value of the function's variable message. You could also type:

print("Print this message")

and that is what it would write to the document.

I hope that's detailed enough for you.