Global scope II (global/block scope)


If ‘Block scope means that a variable defined in the block is only accessible within the curly braces’…

Why is stars being reassigned to Sirius when I call console.log(stars); ?

It makes sense for it to be reassigned to Sirius when I call the function console.log(myNightSky()); as I’m reassigning it myself within the curly braces of that function.

Thank you!

I would like to know too. I even deleted the global var stars near the top and it still displayed the same.

I understand now. You called the myNightSky function first which reassigns var stars. By using let for the var, it can still be reassigned even though it’s global. If you put console.log(stars) before the function myNightSky, you will get ‘North Star’ to display. Am I making sense?

It’s still global because it was declared initially outside of a block. Reassigning a let var in a block does not change it being global as it was declared initially a global variable.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.