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 Foundation Test

In the following code, what value will be displayed in the alert box?

var color = "red";

function example () { var color = "blue"; }

example()

alert(color);

THIS ACTUALLY DOES NOT ALERT ANYTHING AND GIVES AN ERROR

4 Answers

It will say red because inside your function you are declaring a new variable named color at the function scope but the alert is calling the variable named color at the global scope.

It is probably throwing an error because your syntax is missing a semicolon.

var color = "red";

function example() {
    var color = "blue";
};

example();

alert(color);

Actually retested and you are right. It seems though that example(); has no purpose!

Dave McFarland
Dave McFarland
Treehouse Teacher

pgmd

You're right, example() doesn't do anything that affects how the script runs. That's the point here -- the idea is to demonstrate that variables created inside a function have their own "scope" and are independent of variables with the same name outside that function. So even though there's a variable named "color" outside the function and the same variable name inside the function, they're actually two different variables.

This is an important concept in JavaScript -- it makes sure that you can write functions that won't interact badly with other functions. This is really handy when you start combining different scripts -- for example multiple jQuery plugins -- on the same page. You need to make sure that variables don't crash into each other and re-write each other's values;

Great explanation!!! TY

T̶h̶e̶r̶e̶ ̶w̶a̶s̶ ̶n̶o̶ ̶r̶i̶g̶h̶t̶ ̶a̶n̶s̶w̶e̶r̶.̶ ̶ ̶R̶e̶s̶t̶a̶r̶t̶e̶d̶ ̶t̶h̶e̶ ̶t̶e̶s̶t̶ ̶o̶v̶e̶r̶ ̶a̶n̶d̶ ̶o̶v̶e̶r̶ ̶a̶g̶a̶i̶n̶,̶ ̶a̶n̶d̶ ̶c̶h̶o̶s̶e̶ ̶e̶v̶e̶r̶y̶ ̶s̶i̶n̶g̶l̶e̶ ̶o̶p̶t̶i̶o̶n̶.̶ ̶N̶o̶n̶e̶ ̶o̶f̶ ̶t̶h̶e̶m̶ ̶w̶e̶r̶e̶ ̶r̶i̶g̶h̶t̶.̶ ̶ ̶I̶ ̶t̶h̶i̶n̶k̶ ̶i̶t̶'̶s̶ ̶a̶ ̶m̶i̶s̶t̶a̶k̶e̶ ̶o̶n̶ ̶t̶h̶e̶ ̶q̶u̶i̶z̶ ̶m̶a̶k̶e̶r̶'̶s̶ ̶p̶a̶r̶t̶.̶

Edit: There was 2 questions regarding the same code. One was missing var and the other has var. There was a right answer.

Dave McFarland
Dave McFarland
Treehouse Teacher

Hi Kevin Jefferson Souribio

Which quiz are you referring to? Can you give us the URL of the quiz and the quiz question so we can look into this? Thanks.

Hi Dave. Sorry about the inconvenience but I just realized I made a mistake when I looked into it again and found out that there was another question very similar to this one on the quiz. And that they both do have right answers. My bad.

Dave McFarland
Dave McFarland
Treehouse Teacher

No problem Kevin. Glad the quiz works!