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

Daniel Stewart
Daniel Stewart
3,157 Points

Reference enquiry

Hello,

Just quick question.

When he declares the function with the argument (list) is this directly referencing the list array? How is this linked when passing an array into a function?

I know with objects you must use properties from the object to link it to the function like if you wanted update the value of a property with a function.

Thanks, Daniel

2 Answers

nick black
nick black
5,578 Points

When he declares the function with the argument (list) is this directly referencing the list array? How is this linked when passing an array into a function?

It is directly referencing the list array. Since JavaScript doesn't require datatypes in these variable declarations, I think it can be a little confusing to know what is specifically being referenced. I think this is the difference in a loosely typed language vs. strict type, but that isn't important here.

What is important, is knowing that an array with the name list is being referenced as the parameter in this function and so when you pass an argument to the function, it is going to be of an array type. That said, the way this is actually handled by the language is probably using pointers so the array list is actually a place in memory and JavaScript creates an object that points to the memory address that contains the array and that way it isn't passing around the array itself over and over and wasting resources.

Maybe I'm over complicating this, but it seems like you have experience with this concept anyway and that's what you were referencing from your object oriented knowledge.

Cheers!

Steven Parker
Steven Parker
172,335 Points

When you declare a function, you don't know what arguments will be given. Your declaration includes parameter names that act as placeholders for the actual arguments that will be supplied in the call.

In this video, when the function is called, the argument given is "playList". So that is what "list" represents when the function runs.