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 trialGrace Ji
5,402 PointsGiven the code below, what appears in the alert dialogue when this program runs?
var message = "Welcome!";
function setMessage() {
message = "Go away!";
}
setMessage();
alert(message);
I can't understand why the alert message will be "Go away!" I guessed the alert message will be "Welcome!", because the var is out of the function.
4 Answers
Chris Shaw
26,676 PointsHi grace Ji,
The reason why the message isn't "Welcome!
" but instead "Go away!"
is due to how variables work in the global scope versus the local scope. Anytime you omit the var
keyword, JavaScript assumes the variable as been part of the global scope. In the case of this challenge, the value of the message
variable is changed by the function call because we have omitted the var
keyword.
If instead we change this so var
is prefixed to the message
variable in the setMessage
function, it will become a local variable that is independent of the global variable.
Hope that helps.
Grace Ji
5,402 PointsThank you Chris! :)
Robert Rydlewski
3,828 PointsPlease help me understand this !!!
1st Quiz: Given the code below, what appears in the alert dialogue when this program runs?
var message = "Welcome!"; function setMessage() { message = "Go away!"; } setMessage(); alert(message);
Answer “Go away!”
Treehouse - " Because the var keyword isn't used to declare a message variable inside the function, the function overwrites the value in the global variable message"
My understanding lol - "Ok..so the function overwrites the global variable message.. cool next question"
2nd quiz: Given the code below, what appears in the alert dialogue when this program runs?
var name = "Trish"; function setName() { var name = "Sarah"; } setName(); alert(name);
Answer: “Trish” Treehouse - "The name variable outside the function is a global variable, so the alert() (which is also outside the function) accesses global variable."
my understanding " What the minute .. in the previous quiz you just told me the variable inside the function, the function overwrite value in global scope, but now it exactly opposite .. What I am missing ???? Please help!
Grace Ji
5,402 PointsFunction is like a greedy girl.
Let's say ' function' is a little girl, and 'var' is a cookie.
1)f only the girl has a cookie; she doesn't share her cookie with other kids, and eat cookie by herself.
2)If another kid(= another function) has a cookie, and she doesn't have one; she takes the cookie from the kid and eat the cookie.
3)If another kid has a cookie, and she has one also; she takes the cookie from the kid, and save her cookie for later.
Linus Karlsson
7,402 PointsI still don't quite understand this, although the explanation where you use the girl and the cookie analogy is a great way to explain something, Grace =)
What I don't understand comes back to this: if I put the function (or any other code for that matter) inside curly braces { code } what would it matter to the code outside the curly braces if I write "var" or not?
Robert O'Toole
6,366 Pointsill b honest the cookie thing helped me immensely... but this still stumps me. this is the first thing ive been really stumped on in Javascript now :(
Robert O'Toole
6,366 Pointsok i think i finally get it! remember before in a previous lesson once you declare a variable you don't have to write var again? so lets say i make a var message=10..... later i only refer to it as message.
so since the var was not declared the message keyword acts like the gobal variable that was previously declared. IF we wrote var before message in the function, then the function would know to treat it like a separate variable within the function. hope this helps!
Linus Karlsson
7,402 PointsLinus Karlsson
7,402 PointsI did the exact same thing, with the exact same reasoning!