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 trial

JavaScript

Var listHTML is created INSIDE the function but then accessed from OUTSIDE the function....?

Regarding the scope on which variables exist, I thought that if a variable is created INSIDE a function, then it only exists inside that function scope. But in his code, he uses the variable listHTML at the end, outside of the function. So I'm wondering about that. Thanks in advance!

Edit: The video is called 'Two-dimensional arrays' by Dave McFarland. In the video, he uses var to declare the variable inside the function, then references that same variable outside the function. To be clear, it is not a different variable (in a different scope) with the same name.

It would be helpful if you provided a link to the video you are referencing. As you stated, IF a variable is declared inside a function it's scope is limited to the function and a reference to that variable outside of the function should not work.

However, there may be something going on in the video you have not provided that might explain this situation that you might have missed. Without seeing the video, it is impossible to tell.

Steven Parker
Steven Parker
243,318 Points

In which course is this video found?

1 Answer

Steven Parker
Steven Parker
243,318 Points

How a variable is created determines the scope.

If you don't use "strict" mode, you can create a global variable from within a function by omitting the var keyword, though this is not considered good practice.

To get a more complete and specific response please share the code and provide a link to the course page you are working with.

I apologize, I thought I was commenting directly under the video. The video is called 'Two-dimensional arrays' by Dave McFarland. I don't know how else I can reference it. In the video, he uses var to declare the variable inside the function, then references that same variable outside the function. To be clear, it is not a different variable (in a different scope) with the same name.

I don't have any way to copy and paste the code from the video or I would.

Steven Parker
Steven Parker
243,318 Points

To share the link, while you have the video page open, you can copy the URL from your browser's address bar and paste that.

Hi Christopher,

If you use the "Get Help" button below the video then it should link your question to that video.

It looks like this is the video you're talking about: https://teamtreehouse.com/library/javascript-loops-arrays-and-objects/tracking-multiple-items-with-arrays/twodimensional-arrays

I see the listHTML variable declared and used inside of the printList function but I don't see where it's used outside of that.

Steven Parker
Steven Parker
243,318 Points

If that's the correct video, and you're talking about the listHTML variable defined in the printSongs function, I also do not see it being used outside that function.