by grannyg00se · 14y ago · 0 comments
Somehow going from one keyword for variable declaration to three doesn't seem like a good way to reduce errors.
However, they cannot change var's semantics and retain backwards compatibility. So they did the next best thing: introduce a new keyword for variables that work like they should. That keyword is 'let'.
As far as I am concerned, if your JS is known to execute in an environment with 'let', 'var' might as well not exist. Too many subtle errors — “what do you mean, braces don't actually delimit scope?”
"what do you mean, braces don't actually delimit scope?" I would answer, they don't. Because this is not <insert your favourite braces-delimit-scope language>. This is javascript. Nobody should have ever told you that braces delimit scope here.
It's not like this is some extremely rare issue that you aren't going to find documentation for. A quick run through a basic tutorial would suffice.
So how is it that 'let' works the way variables should? It works differently, that's all. So now you have to know how let is different from var, why it was introduced, when to use let, when to use var, etc.
Then when you see code using let, you're going to have to determine whether the person used it because they know what they are doing, or because someone just told them "let is the new var" and they use it all the time. Same for var. There is an added level of uncertainty when reading code. And and added level of complexity in explaining the language. You haven't gotten away from explaining how var works. Because you're still going to see tons of code using it.
Wouldn't it be easier to just learn how to use var properly?
In my opinion, unnecessary surprise as a result of historical implementation artifacts is a design bug.
Yep, need to learn about it. Yep, you can, and write good JavaScript. But that doesn't mean the design is good.
Yes, it's not this that makes it a bug, it all the other horrendous stuff it entails...
Wouldn't it be easier to just learn how to use var properly?
No, it would be easier to fix it. Call it argument by authority, but I trust Brendan Eich more than some random HN dude on this.
JavaScript I not c. JavaScript is not Java. JavaScript is not python. If it was one of those languages it would be named that and there would be no JavaScript books, just dom and browser books.
But it is not those languages. It is different. If people don't learn the language they cannot be expected to write code.
People always bring up var for some reason. Why var? Why not closures? Javascript is often the first place programmers come across this concept and it always causes confusion and bugs at first. Should we get rid of them? Of course not!
And the scoping rules are quite tricky in practice, to real programmers. To take an example from today:
What's worse.... now you still need to learn how var works anyway! It isn't gone. I see 0 reduction in confusion and +3 increase in cognitive load.