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 Object-Oriented JavaScript (2015) Introduction to Methods Understanding this

What is the benefit of removing the var declarations from the method?

Why not just leave them there?

var contact = { fullName: function() { var firstName = "Andrew"; var lastName = "Chalkley"; console.log(firstName + " " + lastName); } }

4 Answers

Steven Parker
Steven Parker
229,771 Points

:point_right: There would not be any benefit to removing "var" declarations.

Why would you expect there might be? Removing them just places the variables in the global scope, which is generally not advantageous.

Current best practice would be to change the var's into either a const or a let, depending on whether the value will be modified or not in the code. This restricts the scope to the one in which the variable is declared.

brandonlind2
brandonlind2
7,823 Points

variables with out var,let or const are globally scoped, var scopes to functions its declared in or globally if not in a function. let which replaced var in the newest update scopes to any code block in {} and const is like let just it cant be changed.

Vincentius Ronalto
Vincentius Ronalto
4,432 Points

any "var" declared outside any function will be global "var", it is good practice always use var something = "else"; so it will be scoped in the place global/ code blocks{}, so if you have same variable name, it won't be overwritten and can also be accessed sorry if my words make you confuse, this is best reference: https://developer.mozilla.org/id/docs/Web/JavaScript/Reference/Statements/var

As others have already said, removing the "var" keyword from the method places the variable in the global scope instead of the local scope. Generally speaking when writing functions, we will want to use variable that are specific to that function (local scope) as well as be able to pull variables from outside of the function (global scope). This is why we declare certain at the beginning of or scripts and declare others within our functions. You run into issue when you leave off the "var" keyword inside of functions because then they can accidentally be altered and break your code when naming a global variable of the same name. The use of "const" and "let" instead of "var" significantly reduces this issue, but it's still considered best practice to declare variables with keywords. If you want to learn more about scope, this Treehouse course might help.