Global scope II (global/block scope)


#1

Link: https://www.codecademy.com/courses/learn-javascript-scope/lessons/scope/exercises/global-scope-ii?action=resume_content_item&course_redirect=introduction-to-javascript

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!


#2

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


#3

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?


#4

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.


#5

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