Is there a reason not to replace 'var' with 'let' in JavaScript?

The question for my seniors in JavaScript is: if you do not take into account the fact that it has a variable scope different from , it seems to me that in the vast majority of cases there is no reason to use over . Of course, if you define a variable inside an operator , this is not the same thing, but is there another reason besides the scope of one on top of the other? letvarvarletif

Edit : To be clear, this question is about best practices (i.e. the pros and cons) in JavaScript, and not about the nature of the differences between varand let.

Thank!

+7
source share
1 answer

No, I think there is no reason. From what I read, varleft for backward compatibility. In most articles I read, the advice is to gradually replace all occurrences varwith let. Of course, this should be done taking into account differences, such as the one you mentioned, the ifblock. I use only letmy ES6 code now. Eric Elliott doesn't use var either. There is nothing using varwhat letto do better. If you need an entire function scope, create letfunctions at the top.

: , var, window.variable, let window. , var window .

const let , .

+8

Source: https://habr.com/ru/post/1657854/


All Articles