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 JavaScript Basics (Retired) Creating Reusable Code with Functions Review: Scope

Brian Martin
Brian Martin
3,773 Points

I'm very confused on this question. Why is the var message not being called?

var message = "Welcome!"; function setMessage() { message = "Go away!"; } setMessage(); alert(message);

I thought I literally just answered a question that stated 'global variables' overwrite the local variables inside a function?

3 Answers

Mark Buckingham
Mark Buckingham
5,574 Points

Hi,

your example simply assigns "Go away!" to the global variable as you do not have a local definition.

If you change your example so that message is declared as a local variable inside setMessage() by adding the var keyword it will no longer affect the global variable, for example:

var message = "Welcome!"; 
function setMessage() { 
   var message = "Go away!"; 
} 
setMessage(); 
alert(message);

This issue arises in what is called "variable scope". In JavaScript, if you want a variable to be accessible within a function, and only that function, you must declare "var message" inside the function. This way, the function knows that "variable_name" is local only to that function. If you declare a "var message" outside of function setMessage, it becomes a "global variable" which means that anything within a block (function, if statement, for loop, etc) can access that "global variable."

Here is some more information on what JavaScript scope is: http://www.w3schools.com/js/js_scope.asp

Mark Buckingham provided a great example of how to localize your scope. By setting var within the function, message can be it's own value within the setMessage function.

var message = "Global variable";

function getMessage() {
    /* message is only local to getMessage() now */
    var message = "local to getMessage";
    /* here is a nested function to show more on scope */
    function nested() {
        /* This will only be visible to nested */ 
        var message = "local to nested";
        console.log("in nested: " + message);
    }
   /* will log "in nested: local to nested" */
    nested();
    /* will log "local to getMessage */
    console.log(message);
}
/* will log:
  * "in nested: local to nested
  *  local to getMessage"
  */ 
getMessage();
/* will log "Global variable" */
console.log(message);

Here is some information on nested functions, and variable scope: http://www.w3schools.com/js/js_function_closures.asp

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

The answer is go away because what is happening is the value of the variable is being reassigned rather then being hoisted. It's the same variable with an overridden value. :-)