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 trialRachael Zordan
1,379 PointsWhy was 'arr' used to pass the array to the function when the array name is 'playlist'?
If there were more than one array in a program, how would the function know which array to pass as an argument? I would have expected the function to begin as:
function createListItems(playlist)
instead of
function createListItems(arr)
Is 'arr' a built-in JS abbreviation so it didn't need to be declared? Or was 'arr' declared as a variable/array name somewhere and I just missed it?
3 Answers
Steven Parker
231,248 PointsParameter names in function definitions only act as placeholders for the actual variable that will be passed when the function is called. They don't need to be related to the actual variable, though it's usually considered "good practice" to use a name that implies the purpose or kind of thing that will be passed in (thus "arr" for an array).
The function doesn't know which array it will deal with until it is called, where the argument will be the real variable name.
kevin hudson
Courses Plus Student 11,987 Pointsyou can put anything in there instead of arr - theAlienDeathArray
. It's just a placeholder to accept a value into the function and use that value for any calculations.
Anastasios Poursaitedes
10,491 Pointsarr is just the parameter name, what will be used inside the function. Parameters hold whatever value we pass as arguments when calling the function. Parameters and arguments are used interchangably but the're not the same. Parameter is the identifier(aka a variable) that will be used inside the function for manipulating or storing info. An argument is what data we give to the function when we call it. You can choose your parameter names and name your parameters whatever you like as long as they adhere to the naming conventions of Javascipt spec.
Michael Kristensen
Full Stack JavaScript Techdegree Graduate 26,251 PointsMichael Kristensen
Full Stack JavaScript Techdegree Graduate 26,251 PointsAn important distinction is that the Function doesn't even know if it is an array that will be sent to it - the parameter become a full variable the moment the Function is called, with the type being 'copied' from the variable that is passed to the Function.