Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Abir Ahmed
PLUS
Abir Ahmed
Courses Plus Student 8,616 Points

How " list.length " provide information about how much items are inside " playList " variable?

Ok, so I understand most of the thing of Javascript so far, but I am stuck now at this point. As you can see in the video Dave used list.length method to give computer information about the lists in playList var, but how list variable can tell how much items are inside playList var, I can't find any logical explanation. If he'd write playList.length instead of list.length then it'd make more sense to me. Can you please explain it?

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I believe I found the part you're referencing at 5:44 in the video and he begins writing a function that looks like this:

function printList( list ) {

}

Here's what's happening. He's going to call/invoke/execute that function and when he does he's going to send it a list. Now that could be any list on the planet. It could be a playList or a todoList or a groceryList or just about anything. But whatever that list is, while this function is executing it's going to be called "list". And now we can access the length of it with list.length. You can sort of think of it as a new variable name.

So if he invokes it like this:

printList(playList);

that's going to call the printList function on the playList. We're sending the playList to the function. And when it comes into the function it gets a new name of "list". And when the function finishes executing you will no longer have access to the variable "list". This is known as "scope" and I promise you will get into more detail on that down the road. I hope that helps clear things up a bit! :smiley:

Abir Ahmed
Abir Ahmed
Courses Plus Student 8,616 Points

Wow, thanks a lot for such a detailed answer :)

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Abir Ahmed You're quite welcome! I wish more people asked questions like this, honestly. Because this is one of those concept things. If you get it now, you'll understand it for the next language... and the next... and the next :smiley:

In the 'Using for loos with arrays' he does use the name of the variable to get its length instead of list. Like this i < students.length. So when is it ok to use the actual variable's name and when do you use list.length, or does it not matter?

Alex Oates
Alex Oates
7,171 Points

Thanks for the answer, this helped me loads as I had the same question!