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

Print Message

Can someone help provide a quick refresh on what the first few line do?

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

  1. What is message?
  2. With the document.write(message), i don't see anything be stored or declared in message?

1 Answer

andren
andren
28,558 Points

What is message?

message is a parameter. Parameters can essentially be thought of as variables that are given a value when the function is called. For example if the function is called like this:

print("Hello World");

Then message would be set equal to "Hello World". So whatever is typed into the parenthesis when calling the function (technically called an argument) is assigned to the parameter that you specify in the parenthesis when defining the function.

With the document.write(message), i don't see anything be stored or declared in message?

This is pretty much explained above. Parameters are not assigned a value within the function they are used in since they exist to be assigned a value when they are called from outside the function itself. Assigning a value to the parameter inside the function would in fact be quite counter intuitive as it would override whatever was passed into the function.

So to summarize the parameter of the function message is set to whatever argument you pass into the function when it is called . It's worth noting that you can have multiple parameters and arguments being passed into a function, in that case the arguments are assigned to the parameters based on their position.

Here is an example of a function with two parameters:

function add(num1, num2) { // num1 is the first parameter, num2 is the second
    return num1 + num2
}
add(5, 10) // 5 is the first argument, 10 is the second

The first argument (5) is assigned to the first parameter (num1). and the second argument (10) is assigned to the second parameter (num2). So the result of the function is 15.