Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

Since according to What's 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 'for (var i=0...' the actual correct way should be to use let? I can't imagine a case where a developer who was using 'for (var i=0...' would want to have the var i still be visible outside the for loop, meaning that all 'for (var i=0...' are wrong and the correct way is 'for (let i=0...'? Just a yes or no question.

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
};
like image 287
eternalminerals.com Avatar asked Aug 31 '15 14:08

eternalminerals.com


People also ask

What is the difference between VAR and let in for loop?

The main difference is the scope difference, while let can be only available inside the scope it's declared, like in for loop, var can be accessed outside the loop for example.

What is the difference between VAR and let in JS?

let is block-scoped. var is function scoped. let does not allow to redeclare variables. var allows to redeclare variables.

Should I use VAR or let in JavaScript?

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. Do not use var.

Can var be used in for loop?

Use var x in your for loops. To top it all off: if the for loop is in the global scope (i.e. not in a function), then the local scope (the scope x is declared in if you use var x ) is the same as the global scope (the scope x is implicitly declared in if you use x without a var), so the two versions will be identical.


1 Answers

let is introduced in Ecma Script 6 - the new javascript version - which is still in development at the time of this writing. Therefore, using var will get you across more browsers for the time being.

On the other hand, I urge people to use let instead of var from now on. I would go ahead and list the reasons but you have already have a link in your post with a great explanation in it. Long story short, let helps you avoid the variable hoisting in javascript and keep your variables' scope at just where it needs to be.

update

I've forgotten about this post until I recently got an upvote. I'd like to update this statement. I personally use const as much as I can these days. If you get into linting your code, which I highly recommend, and use something like airbnb lint rules, it will also tell you to use const. It will make your variables a constant so you will not be able to mutate them once you set their value thus it is not applicable in all cases. const and let also have advantages in terms of scope over var and it is well worth a read. My hierarchy of usage goes as such const > let > var

like image 122
ODelibalta Avatar answered Sep 30 '22 00:09

ODelibalta