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 trialFabiano Brandão
2,176 Points[SOLVED] Advantages of global variables
In which situations global variables are better than local scope? What are the advantages of using them over local variables?
<!DOCTYPE html>
<html lang="en">
<head>
<title> JavaScript Foundations: Variables</title>
<style>
html {
background: #FAFAFA;
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>JavaScript Foundations</h1>
<h2>Variables: Hoisting</h2>
<script>
function elevatorCloseButton(pushed) {
if (pushed) {
var status = "I'll close when I'm ready.";
}
}
elevatorCloseButton(true);
</script>
</body>
</html>
Iain Simmons
Treehouse Moderator 32,305 PointsKristi Smythe, you should include your answer as an actual answer to the question, rather than a comment, so the person asking can mark it as the 'best answer' and the question will be noted as being 'answered'... particularly when you're providing such excellent advice! :)
Marcus Parsons
15,719 PointsI agree with Iain Simmons. Kristi Smythe that was excellent advice, and if you can, in the future, put your answer as an answer instead of a comment. But no matter, I'm going to mark this as solved since you provided such a well thought out and well spoken answer.
Fabiano Brandão
2,176 PointsThank you for helping me out, Kristi. =)
Kristi Smythe
10,665 PointsKristi Smythe
10,665 PointsHi Fabiano,
Great question!
It's my understanding that global variables can toss you into a debugging nightmare in a complex application.
Also
var
is the dredd pirate 'optional' in JavaScript. Omittingvar
when we're creating our functions causes a variable to escape and go global, like a bubbling volcanic eruption on a 'mess up your app' spree.This also includes not utilizing
var i = 0;
for your iterators in a loop. You can imagine that if you have more than one of these loopy littlei
suckers messing around outside their functions, it could drive you nuts trying to find out why your hard wrought masterpiece is misbehaving.To quote a convo on this topic from Stack Overflow, "This [omitting var] is, in my opinion, one of the most dangerous issues with javascript, and should be deprecated, or at least raise warnings over warnings. The reason is, it's easy to forget var and have by accident a common variable name bound to the global object. This produces weird and difficult to debug behavior." Source: http://stackoverflow.com/questions/2485423/javascript-is-using-var-to-declare-variables-optional