Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 994
Vasiliy Vanchuk Avatar asked Aug 16 '16 20:08

Vasiliy Vanchuk


People also ask

Is it better to use const or let?

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.

Is let faster than const?

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.

Can we use const inside a function?

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.

What is difference between const and let?

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.


1 Answers

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.
like image 142
Max Brodin Avatar answered Nov 12 '22 16:11

Max Brodin