This is useful for the novices among us, especially because it's extremely easy to tell someone "don't use var" and not really follow up on why.
But one thing I'd like to know more about is how const and let are handled under the hood (at the engine level). I have some (probably wrong) theories about what happens, but I'd like a comprehensive explanation if anyone has a good link.
I'm not sure what happens at the engine level, but also not sure that it matters. As far as I know, Babel translates all `const` and `let` statements to `var` [1]. There's not really any special interpreter functionality required by const because it behaves just like var but with some lexical rules (enforced by the transpiler) and some slight differences in scoping which can also be erased by the transpiler.
I rarely use Javascript. When I do, I try to just not put anything in front of a variable I set, like in Python. I think this makes it global, but I'd rather remember that and deal with the consequences than remember the other rules. Of course, if I used it professionally or on a collaborative project I'd have to change that.
This articles makes me wonder why `var` is still a thing. Is it deprecated (or slated to be deprecated soon)? If not, why?
You can basically consider `var` to be deprecated. It's old and there's no reason to use it. That said, it will be supported forever because of the need to support old browsers and old websites. Modern JS codebases use a linter which will tell you not to use `var`.
The rule is pretty simple: use `const` always, unless you can't because you are doing a reassign, in which case use `let`.
4 comments
[ 5.1 ms ] story [ 18.1 ms ] threadBut one thing I'd like to know more about is how const and let are handled under the hood (at the engine level). I have some (probably wrong) theories about what happens, but I'd like a comprehensive explanation if anyone has a good link.
[1] http://shorturl.at/gAKOU
This articles makes me wonder why `var` is still a thing. Is it deprecated (or slated to be deprecated soon)? If not, why?
The rule is pretty simple: use `const` always, unless you can't because you are doing a reassign, in which case use `let`.