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 trialAmy Dusek
4,346 PointsUsing "var" when creating new Person instances?
I noticed that he doesn't use the 'var' keyword when creating a new Person instance: Ex) jim = new Person("Jim");
Should it be: var jim = new Person("Jim"); or does it not matter in this scenario?
1 Answer
Geoff Parsons
11,679 PointsThe use of var
is optional though highly recommended when doing anything beyond the most simple script. The MSDN documentation gives a pretty good explanation:
Use the var statement to declare variables. You can assign values to the variables when you declare them or later in your script.
A variable is declared the first time it appears in your script.
You can declare a variable without using the var keyword and assign a value to it. This is known as an implicit declaration, and it is not recommended. An implicit declaration gives the variable global scope. When you declare a variable at the procedure level, though, you typically do not want it to have global scope. To avoid giving the variable global scope, you must use the var keyword in your variable declaration.
In the example Jim Hoskins was using in the video this doesn't really matter all that much but you're right, using var
is a good habit to be in.
Amy Dusek
4,346 PointsAmy Dusek
4,346 PointsThank you! I appreciate the clarification!