JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Using For Loops with Arrays

Trajce Nastevski
Trajce Nastevski
3,997 Points

Are 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
Mikel Cati
8,044 Points

Hello 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. Ive 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
Steven Parker
203,206 Points

Parameters 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
Aayush Mitra
24,889 Points

Hey 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
seal-mask
.a{fill-rule:evenodd;}techdegree
immad Ud din
Front End Web Development Techdegree Student 11,599 Points

Hi, 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.