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
behar
10,800 PointsJavascript variables
Hey guys! Quick question about variables in Javascript. So you can use var, let and const to set variables. Dont use var if you can avoid it. Use let if the variable can change and const if it cant. But then i noticed that you can actually declare variables just like you do in python. For instace:
test = "whatever"
That variable is just as valid as far as i can tell. One reason i am using it is because it seems that if i declare a variable with let inside a function, i cant acces that variable from outside the function, but i can if i declare it like above. Is there any reason why i shouldent use this kind of variable, and why have i never heard about it?
1 Answer
Steven Parker
243,658 PointsWhat you have there is an "implicit global declaration", which is not considered a good programming practice. If you really want a variable to be global, it should be declared outside the function using an appropriate declaration word.
Also, if your program is running in "strict" mode, implicit declarations become a syntax error.