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 D3.js Selections with D3.js SVG Graphics

Yuttanant Suwansiri
Yuttanant Suwansiri
1,608 Points

why was mySVG variable not declared with var in front of it?

have this question on d3.js tutorial from this link https://teamtreehouse.com/library/d3js/selections-with-d3js/svg-graphics

1 Answer

Ionut Costica
Ionut Costica
737 Points

I don't think there was any particular reason to it as he's not re-using the variable outside the function it's declared in. He probably just forgot the "var". (It works, because if you don't put in the "var" in cases like these, it declares a global variable instead of a local one. For more information see here under heading 'Automatically Global')

Yuttanant Suwansiri
Yuttanant Suwansiri
1,608 Points

Thank you so much for your answer, that makes sense now so it would automatically becomes a global variable, I learn something new, thank you :-)