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 trialmoath alqutoob
2,450 Pointsi 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
11,521 PointsThis 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.