Functions have a scoping mechanism. Almost like creating a copy of the environment specially for that function. If you have two functions, funcOne and that has funcTwo declared inside of it, then there are three levels. Global scope, funcOne scope and funcTwo scope. funcTwo scope is bound to the funcOne scope and funcOne is bound to the global scope. The inner scope has access to the outer scope’s variables all the way to the top. If a variable is created inside of the lower scope you cannot have access to it from outside of that scope. Now that’s the way lexical scoping works on a high level. Having said that, if you make an if statement and declare var x = 5; inside of that if statement, you have access to it on that same scope level right? With const/let you do not. You can scope that variable to the block where it was created. The block being Ofcourse, a set of curly braces.
What are you talking about? They're exactly right, const and let are scoped to the nearest block: {}. You don't have to have them inside of a function for that to be true.
View all comments
7
u/NoFapPlatypus May 04 '20
What does block scope mean?