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
Antonio Ascue Avalos
3,023 PointsWhy did my variables work despite that they were not declared with var?
So here is my code https://teamtreehouse.com/workspaces/40819478#. I OVERDID IT more than necessary but just to make it more interactive for the user. I also did not include any comments so that's missing. Another thing I forgot was to DECLARE THE VARIABLES, but the program still worked. My question is WHY and what is really my undeclared variables?
Antony .
2,824 Pointsthanks for clarifying that Steven Parker!
3 Answers
Antony .
2,824 PointsUnfortunately sharing your code this way won't show for other users. Open your workspace and on the top right click on the camera icon, called a snapshot to make a reference link to your workspace.
Steven Parker
243,266 PointsNormally, JavaScript performs "implicit global declaration" on variables that are assigned without being declared. So anything you did not declare becomes a global, even if it is only used within a function.
In the "strict" mode (enabled by the "use strict"; directive), implicit declarations are not allowed, and if you forget a declaration it will cause an error.
Matthias Margot
7,236 PointsHi Antonio,
I have a link for you that could help with clarification of that (it's a bit tricky to understand and involves something called 'scope', if you've never heard this before don't worry if you don't understand it right away, took me some time to wrap my head around but it's not that scary anymore once you get the gist of it):
http://scribu.net/blog/javascript-var-keyword-for-php-developers.html
Steven Parker
243,266 PointsSteven Parker
243,266 PointsA direct workspace URL is only temporary. To get a persistent one, create a snapshot of your workspace and post the link it makes instead.