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

How does the quiz print function variable work?

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

How come it has the variable message in it? I don't understand. Is message a flexible variable or a function level variable created just for this function?

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Eric,

The variable message is what's known as a parameter, the keyword parameter extends to all languages and is used as a way to pass data into a function and/or method without requiring it to be declared in a global scope somewhere else in the code base.

To put this into perspective see the below.

var foo = 'foo';
var bar = 'bar';

// Both `foo` and `bar` are both global variables (assuming our scope here is `window`)

function myFunction(foo, bar) {
  // Here we seem to have declared `foo` and `bar` again, however, now they are locally scoped parameters
  // in-which are separate from the globally declared variables allowing for unique values
}

myFunction(foo, bar);
// Uses the global variables `foo` and `bar` as arguments

myFunction('foo', 'bar');
// Uses static strings as the arguments, no different to the first `myFunction` declaration

Hope that helps.

Vance Rivera
Vance Rivera
18,322 Points

100% correct! You can think of parameters as already declared variables that are scoped to it's function and in order to execute you must supply the function with the value of the parameter. So in your case when you call the print function you must also supply it with a string or a variable that contains a string so the function can write it to the page. Your call will look like this print ('Hello World'); or var message = 'Hello World'; print(message);