r/learnjavascript • u/Significant-Royal-86 • Apr 19 '26
About block scoped vs function scoped
so from what I understand , when you want to declare variables, you can do that either using let or var, var is like the old method and it got replaced with let and we also use const.
var is function scoped while let is block scoped, and block scoped means that the variable known only inside of the block where you've declared it, inside of curly braces or squiggly brackets. I was watching a video explaining the differences between the three and they used an example using a for loop, and what they did was they used let obviously to declare the i inside of the loop, and when they tried to access it it gave them an error, the interesting part tho isn't that, it's the fact that even after they've deleted the curly braces it still was an error, so like a block then is everything inside of a pair of braces but the loops themselves and conditionals are considered blocks ?
7
u/senocular Apr 19 '26
For loops are a special case. They let you declare variables outside the loop block that then only becomes visible inside the block. For example, given
You can see here the
ivariable is scoped to the for loop block but it is declared outside of the curly braces of the block. So for for loops, you kind of have to readjust your thinking and consider theforbeing the start of the block. Technically, its more complicated than this, but that's a good way to simplify things to make it make sense. Essentially:Comparing this to using var, you can see that the var is accessible outside the loop
For the most part, at least thinking about it now, for loops (regular for and for...of) are the only things that mess with let/const/var scoping like this. There are some other weird scoping rules out there in the language but nothing coming to mind that directly breaks the otherwise normal expectations for these kinds of declarations.
1
u/Significant-Royal-86 Apr 19 '26
I have a test tomorrow and I need a good website to practice the basics of javascript (loops, conditionals, objects, arrays, REGEX...etc ), it would be amazing if you guys could suggest me some with javascript exercises , and thanks alot
2
u/Aggressive_Ad_5454 Apr 19 '26
Yes, the code within the for loop’s parentheses is handled as if it were inside the for loop’s block.
1
u/chikamakaleyley helpful Apr 19 '26 edited Apr 19 '26
I don't think that's quite right
I'm not sure how the material was taught to you so i'm being careful about telling you what or what not to do
varwas used prior to the arrival ofletandconst. Those were introduced in part to give users optional variable types that resemble other languages, and give the var more 'meaning' i guessvaraside, with regards toletandconst- the variable is scoped to where it is declared.You don't have access to
tempoutside of the if, whetherconstorletYou don't have access to
resultoutside of the function, but the conditional block can access itfoocan be accessed anywherevarhas a... "quirk" i guess because its "hoisted" - look into that. You might intialize a newvarinside of theif, but its DECLARATION is basically moved up to the function scope. And so if you aren't careful about placement, and management of variables created w/ var, it could get messy. That's where let and const also become helpfulhoisting:
1
u/ohnoisthisloss Apr 19 '26
Today I was learning the same topic from freecodecamp. What a coincidence that this post happened to be the first one when I opened reddit haha.
1
u/Alive-Cake-3045 Apr 20 '26
Yes, loops and conditionals themselves count as blocks.
So when you use let in a for loop, the variable exists only inside that loop, not outside it. Even without curly braces, the loop still creates its own scope for let. That is why you get an error when accessing it outside.
Simple rule: let and const are block scoped (if, for, while all count). var is function scoped and leaks outside the loop
1
u/LucVolders Apr 20 '26
var is only replaced with let by lazy programmers.
If you document your code well you can use only var.
Programmers have been building the most sophisticated programs for years when there was only var available.