Why should I use let instead of const inside functions? [closed]
Asked Answered
M

1

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

Microcurie answered 16/8, 2016 at 20:48 Comment(3)
+1; I have no idea why anyone would want that. You should always use const unless you actually need a variable to change.Warren
Seems all very opinionated to me.Dow
I think adding const inside a method is ridiculous? What is the point? To let yourself and others know the value will not change? stupid addition in my opinion and I'm tired of seeing it.Fayre
A
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.
Attired answered 17/8, 2016 at 0:50 Comment(2)
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.Pyuria
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 directrlyMicrocurie

© 2022 - 2024 — McMap. All rights reserved.