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?

2 Answers

Steven Parker
Steven Parker
182,288 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,886 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!