(no title)
SHOwnsYou | 9 years ago
let x = 'outer';
function test(inner) {
if (inner) {
let x = 'inner';
return x;
}
return x; // gets result from line 1 as expected
}test(false); // outer
test(true); // inner
This makes it seem like let creates global variables. Why would you want to return a variable from outside the function? Doesn't that create massive overhead in terms of keeping track where variables are initially set? Easy to understand in this example, but what if let x = 'outer'; is defined at the top of a 5000 line script and this function appears near the bottom?
Edit: Turns out I don't know how to format code. This is in the first example of section 3.1 Block Scope Variables.
Meegul|9 years ago
This is mostly just a case of 'let' restricting a variable to the block it is in, and the child blocks. In your example, `let x = 'outer';` is sort of acting like a global variable, but the importance is that if another script were to be running, it could not access that instance of 'x'.
Roboprog|9 years ago
Somebody writing stuff, into the global namespace, directly in <script> tags, has bigger problems :-)
SHOwnsYou|9 years ago
I'll read more into uses of let over var. Function level scoping a la var feels like less mental overhead, but as I read more I'm sure my opinion will change.
unknown|9 years ago
[deleted]