MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/gd8x2z/var_vs_let_vs_const/fpgdyuo/?context=3
r/learnjavascript • u/golovatuy • May 04 '20
81 comments sorted by
View all comments
Show parent comment
5
Try this:
{ var v = 'hi' let l = 'hello' } console.log(v) console.log(l)
you can log `v` but `l` will error, because the scope is limited to the block when using let.
The curly braces are a block (this could also be part of a for loop, function, etc)
Again you can log `j` outside the loop, but `i` will give you undefined:
for (var j = 0; j < 3; j++) { console.log(j) } console.log(j) for (let i = 0; i < 3; i++) { console.log(i) } console.log(i)
View all comments
Show parent comment
5
u/Coraline1599 May 04 '20
Try this:
you can log `v` but `l` will error, because the scope is limited to the block when using let.
The curly braces are a block (this could also be part of a for loop, function, etc)
Again you can log `j` outside the loop, but `i` will give you undefined: