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 trialMartin Grogan
5,980 PointsThe answer is True but only if .... Maybe specify that 'if' in the question because I answered False BECAUSE of the 'if'
change the answer to the question or change the question itself, because it is confusing :P
4 Answers
Samuel Webb
25,370 PointsThe key words are "DECLARE" and "WITHIN". The answer is True. Declaring a variable means using the var
keyword to make it. Meaning inside of the function you would type var whatever = whatever
and if you do that it is only accessible from within the function.
But if inside of the function you just type whatever = whatever
, you're technically declaring the variable outside of the function and that makes it a global variable.
The question and answer are both correct.
function something(){
var hello = "world"; // Declaring a variable inside of a function. only accessible in that function
}
function somethingElse(){
hello = "world"; // This actually gets declared outside of the function which makes it global.
}
The confusion comes from not having a complete grasp on the actual complexity of scope in JavaScript.
Samuel Webb
25,370 PointsWhat question are you talking about?
Martin Grogan
5,980 PointsQuiz Question 3 of 5 When you declare a variable within a function, that variable is only accessible within that function.
The real answer is should be False, not True
JavaScript basic Quiz | Review Scope
Martin Grogan
5,980 PointsI see the point, Thanks for answering so fast !
Samuel Webb
25,370 PointsGlad to help. This was quite a confusing thing to me when I first got started with JS.