8

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

Community
  • 1
  • 1
Vasiliy vvscode Vanchuk
  • 7,007
  • 2
  • 21
  • 44

1 Answers1

3

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:

  1. Aggressive use of const devalues the operator
  2. 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?
  3. There is no known performance difference between let and const.
Max Brodin
  • 3,903
  • 1
  • 14
  • 23
  • 8
    The notion that aggressively using something for what it was designed could be through of as somehow "devaluing" it is frankly, really weird. I find that I use `const` overwhelmingly, so I don't have to "stop to think about every declaration", and if it happens to be a variable I do want to modify, my editor/compiler tells me immediately so I can change it in two seconds. –  Aug 17 '16 at 04:29
  • About p1 and p2 - Then I can use `var` everywhere. Using const ( and types in TS ) allow to decrease number of errors, I suppose - If I'll redefine some variable I'll say about it directrly – Vasiliy vvscode Vanchuk Aug 17 '16 at 06:21