3 ms·
Firstly (compared to python at least) there's the obvious 'typo in variable name barfs early' advantage, which honestly is not to be sniffed at on its own. But
by mst 3y ago
Firstly (compared to python at least) there's the obvious 'typo in variable name barfs early' advantage, which honestly is not to be sniffed at on its own.
But my big thing is about being able to scope values to the specific block where they belong:
...
let foo;
{
let x = blah();
let y = frob(x);
foo = munge(y);
}
and the temporaries 'x' and 'y' have gone away as soon as the } happens.
also, if you've ever had "fun" trying to capture a loop variable,
for (let val of things) {
callbacks.push(() => val.finish());
}
has a different 'val' for each iteration of the loop so you can do that safely without requiring some sort of copying or an intermediate function invocation to create an extra scope.
Oh, and it also renders impossible certain weird constructs like
if <test>:
some_name = ...
where now whether the some_name variable even -exists- after that if block is indeterminate without knowing which way the <test> went.
I also like being able to do e.g.
for (let thing of things) {
let blah = thing.blah;
callbacks.push(() => blah.doThing());
}
Without 'let' you'd have to write (for the first 'for' example)
for (var val of things) {
callbacks.push(((val) => {
return () => { val.finish() }
})(val));
}
to perpetrate let-over-lambda to get a separate scope for the callback function.
This is not my favourite thing to write, and -very- far from my favourite thing to read later.
Hopefully that gives you a better idea of why I, at least, prefer having 'let' available.