'let' vs 'var' in javascript for loops, does this mean that all for loops using 'var i = 0' should actually be 'let i = 0' instead?

Since according to What is the difference between using "let" and "var" to declare a variable? The let keyword has a smaller scope than var when used in a for loop. Does this mean that, of all the places where the "for (var i = 0 ..." really correct method should be used, say? I can’t imagine the case where the developer who used 'for (var i = 0 ... 'I wish that var I could still be seen outside the for loop, which means that all' for (var i = 0 ... 'are wrong, and the correct way is for (let me = 0 ... "Just a question" Yes or no ".

function allyIlliterate() { //tuce is *not* visible out here for( let tuce = 0; tuce < 5; tuce++ ) { //tuce is only visible in here (and in the for() parentheses) }; //tuce is *not* visible out here }; function byE40() { //nish *is* visible out here for( var nish = 0; nish < 5; nish++ ) { //nish is visible to the whole function }; //nish *is* visible out here }; 
+6
source share
4 answers

let is introduced in Ecma Script 6 - a new version of javascript - which is still under development at the time of this writing. So using var will show you more browsers.

On the other hand, I urge people to use let instead of var from now on. I would go further and list the reasons, but you already have a link in your post with a great explanation in it. In short, let helps you avoid dragging and dropping a variable in javascript and keep the variable area in the place where it should be.

Update

I forgot about this post until I recently got an inscription. I would like to update this statement. I personally use const as much as I can these days. If you delve into your code, which I highly recommend, and use something like airbnb lint rules, it will also tell you to use const. This will make your variables permanent, so you won’t be able to mutate them once you set their value, which is why it is not applicable in all cases. const and let also have volume advantages over var , and it's well worth reading. My usage hierarchy goes as such const > let > var

+7
source

The answer is no, in my opinion:

 function doSomething( anArray ) { for (var i = 0; i < anArray.length; ++i) { if (someTest( anArray[i] )) break; } return i === anArray.length ? "test failed" : "test succeeded"; } 

This is not the most beautiful thing in the world, but checking the iterator variable after the loop is at least somewhat idiomatic. It is also possible that a var i in one for loop is used in the same function in a later for loop that does not repeat var .

Now, whether code needs to be improved in such a way as to use let is another question. However, it should be clear that blindly replacing such use of var with let in the existing code base would be seriously unreasonable.

Going forward, of course, it’s a good idea to limit the scope to as limited as possible, but dogmatic rules like “always use let , not var in for loops” cannot take into account all the programming problems you may encounter. “Best practice” is “best” so far.

+6
source

Yes, you would like to use let there, since it will only be bound to this block.

In addition, using let this way allows you to avoid accidentally creating a closure if you call a function in your loop and pass a counter as a parameter.

EDIT: Perhaps this should be better put this way: ask yourself if you need to access the value of the variable outside the loop. If you do, use var ; in all other cases, use let .

+5
source

The answer is no. You just have to avoid for loops. Check performance tests or, for example, answer here .

Going to a point with the let keyword - yes, you should definitely use it if your logic does not mind. However, this is actually not too much. You usually overwrite the value of your variable within the same area, so the only thing it gives you is more secure programming.

+1
source

All Articles