I add linters for js (es6) in my project, and for new configurations I found that they prevent using const
inside functions - only for module-level constants. And inside functions I should use let
. But I can't find any ground for such rule. Why?
For jscs it's for example
disallowConstOutsideModuleScope:
const
should only be used in module scope (not inside functions/blocks)
I understand that I can configure and switch off that rule, I just wonder what for it was enabled ? What is the motivation for such checking?
P.S. I have link https://madhatted.com/2016/1/25/let-it-be with block "Constantly const"
There is another school of thought on when to use let and const I’ll need to address. This strategy suggests developers use const as much as possible. Any variable that is not re-assigned should be declared with const.
I think this usage is poor practice. It adds an extra distraction to the process of programming, and results in code difficult to understand and change.
But I can't find that arguments valuable
Summary. As a general rule, you should always declare variables with const, if you realize that the value of the variable needs to change, go back and change it to let. Use let when you know that the value of a variable will change. Use const for every other variable.
Clearly, performance-wise on Chrome, let on the global scope is slowest, while let inside a block is fastest, and so is const. First of all, the aforementioned benchmark tests are performed by running a loop 1000 x 30 times and the operation performed in the loop was appending a value to an array.
A function becomes const when the const keyword is used in the function's declaration. The idea of const functions is not to allow them to modify the object on which they are called. It is recommended practice to make as many functions const as possible so that accidental changes to objects are avoided.
Introduction to the Difference Between Var, Let, and Const Here if we will declare a var variable or let variable, then it can be updated, but if we declare a const variable, it will not be updated in any case and will work fine with our function.
It's just a coding guideline. They follow this school of thinking. If you do not wish to use it, feel free to turn it off in your .jscsrc
file. Main points are:
- Aggressive use of const devalues the operator
- Choosing const first really means choosing to think about every declaration. Will the next line of code change this assignment? How will I use this variable?
- There is no known performance difference between let and const.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With