The compilers are smart. Let them do their thing.
The compilers are smart. Let them do their thing.
It's nice when looking at someone else's code and being able to tell if they intended to have a variable be reassignable or not.
Maybe you've talked to JavaScript devs who are just bad at explaining their reasoning or maybe they just strictly adhere to linters which encourage the use of const and let and throw linting errors when they come across a var.
Either way, I'd recommend you reevaluate your language and refrain from calling other dev's weenies just for having a different opinion than yours and maybe not being able to properly explain their stance. Using language that borders bullying is not helpful, it's no better than calling someone a snowflake or other degrading term.
It's not about the compiler.
More to the point, "const" in JS is a maintainability tool to prevent accidental reassignment. I don't think people use it for performance reasons, nor should they.
var foo = 0;
eval("foo = 1");
With that said, virtually nobody uses `const` for performance reasons, but for developer readability and avoiding mistakes.Given code like `function foo(fn, str) { var foo = 0; fn(str); return foo; }`, you might be wondering what happens if foo is called with `foo(eval, "foo = 1");`. The answer is that a standard eval call isn't actually a normal function call but better understood as a special syntax resembling a function call. There's a big difference between `eval(x);` and `var e = eval; e(x);`. When you take the value of `eval` (by passing it as a parameter or assigning it to a variable, etc), the value you get is actually the "indirect eval" function, which works slightly differently than standard eval: it does not get access to the local scope that it's called in. This means its usage won't break any optimizations that assume the local scope won't be modified.
They can assume it, they just need to be able to reverse that assumption if you do change it through something like eval. This is called 'speculative optimisation' and we use it to optimise languages like JavaScript, Ruby, Python, etc.
const user = {id: 1, status: 'active'};
user.status = 'disabled';
is valid javascript const user = Object.freeze({id: 1, status: 'active'});
But const is immaterial to this topic, as it only prevents you from being able to reassign user to some other value, i.e., user = 7;
While... let user = Object.freeze({id: 1, status: 'active'});
user.status = 'disabled'; // throws an error.
Provides the behavior that you're seeking to guard against, regardless of let/var/const usage.No, I'm talking about the behavior of const.
final int[] arr = {1, 2, 3};
arr[1] = 10;
is valid Java.Javascript could use some proper immutable types.
It's also documentation of intent.
Some code, for better or worse has very long functions, and you might use the variable 100 lines down from where it is declared.
Is a runtime error better than letting it silently change? Debatable, but the error is more likely to blow up a unit test, so you know there is a problem before you ship.
And needless to say: in Typescript you'll get a compile time error. In Typescript it's a no-brainer to use const when appropriate.
Seems like a massive overstatement. What are you even referring to as the downside?
For 99% of usecases/people, TDZ just means you get a runtime error for your use-before-declaration bugs.
The only reason I can see for using `var` is if you're doing some very confident performance hacking in a hot loop and you have the performance testing harness to prove that you're not just wasting your time.
The compiler can, I can't (efficiently). const spam also occasionally catches when I modify the wrong variable.