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!

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 Foundations Variables Shadowing

myFunc variable scope global vs inside function

When I execute following code (Chrome and Firefox) and I still get variable myColor value set to yellow. Am I doing something wrong? Code:

    <// Shadowing
    <var myColor = "blue";
    <console.log("myColor before myFunc", myColor);
    <function myFunc() {
<  myColor = "yellow";
<  console.log("My color inside myFunc", myColor);

    <console.log("myColor after myFunc()", myColor); 

Console output: <myColor before myFunc blue myscript.js:62 <My color inside myFunc yellow myscript.js:66 <myColor after myFunc() yellow myscript.js:70

2 Answers

Sorry my mistake I forgot to use var declaring variable myColor inside the function.

Benjamin Gooch
Benjamin Gooch
20,366 Points

I did the same thing and I'm glad you figured out your mistake! If you hadn't, I'd still be trying to figure it out. JS is way more complex than CSS or HTML, as I am learning.