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 trialTrajce Nastevski
3,997 PointsAre variables defined in the parameters of the function?
In trying to understand this lesson I noticed that when the functions print and printList are defined the parameters in them are not previously mentioned. I thought that the initial parameters of the function are a variable that needs to be previously defined. Can you please share if I got something wrong here.
Eg. the message parameter is not previously defined anywhere in the code.
function print(message) { document.write(message); }
I probably misunderstood something here, can you help me out with this?
4 Answers
Mikel Cati
8,045 PointsHello there, i was struggling as well understanding this and i still do it`s okay . You got some great replies from the Treehouse community already. My way of approaching this issue was to think of like this:
function printList ( list ) <== // The list here does not have any value yet it`s like an undefined variable ) {
let listHTML = '<ol>'; for ( let i = 0; i < list.length; i += 1 ) { listHTML += '<li>' + list[i] + '</li>'; } listHTML += '</ol>'; print( listHTML ); }
printList( playList ); <== // Here the value of the list is whats stored into the variable of the playList.)
// We could have called the parameter ( list ) a different name it does not matter as long as you assign the value of the playList. I
ve changed it to the name of 'dog' and it works the same. You can take a lot here : ``` javascript
function printList( dog ) { let listHTML = '<ol>'; for ( let i = 0; i < dog.length; i += 1 ) { listHTML += '<li>' + dog[i] + '</li>'; } listHTML += '</ol>'; print( listHTML ); }
printList( playList );
Ps: I tried to make it really simple, i apologise if i maybe confused you more but this is really what helped me understand this.
Steven Parker
231,268 PointsParameters are declared when they are named in the function definition. For example:
function print(message) { // <-- this declares "message" for use inside the function body
If a previously declared variable by the name "message" exists, the parameter will "shadow" it (make it not accessible) inside the function.
Aayush Mitra
24,904 PointsHey Trajce! I agree with my friend Steven.
In the parameters of a function you state the name of a variable that you will use in the rest of your function.
function print(message) {}
But this variable is not supposed to be defined earlier in the code. This variable is defined when you call the function:
print("Hello World!");
I hope this helps you out!
immad Ud din
Front End Web Development Techdegree Student 14,756 PointsHi, You can define a variable outside the function first but that variable will be in the global scope and other functions can access and change that too. e.g you can write your code like that
var message = "Hello, there"; function print( ) {document.write(message);} print();
In this situation, you don't need to declare the parameter because parameters are in the scope of the function and only that particular function can access and change that variable. so if you want to share the variable among different functions place it in the global scope like this code otherwise use it as a parameter.