'Reassign javascript with let

Are there any differences between the two ways described below? Which one should I use any why?

while (true) {
    let test = getValue();
    ....
}

And

let test;
while (true) {
    test = getValue();
    ....
}


Solution 1:[1]

If you use test variable outside your while loop, go with the second:

let test;
while (true) {
    test = getValue();
    ....
}
if (test) { ... }   // <--- Use test outside the while loop

Otherwise, the first one is better

Solution 2:[2]

Yeah there is a difference in the first version you cannot access the variable test outside of the while loop. In the 2nd version you can access the test variable outside of the loop.

Solution 3:[3]

Yes, there are differences:

  • In the first example, you have a separate test variable for each loop iteration; in the second case, you have only one test variable shared by all loop iterations. This has important effects if you create any functions in the loop, and can have a performance aspect (though of course, like all performance aspects, it's only a problem when it's a problem).
  • In the first example (each) test variable is only accessible within the loop; in the second, the one test variable is accessible outside the loop as well.

Which one should I use any why?

The right one for the situation. If you need a separate test variable for each loop or just want it to be private to the loop, use the first; if not, use the second.

Solution 4:[4]

In the first version, variable's scope is limited in while loop while in another one variable can be accessed outside while loop.

Solution 5:[5]

Yeah, there are differences between both of them.

  • If you declare the variable outside the loop, it can be updated in the loop & then be accessible in other code outside the loop.

  • And if you declared it in a loop, it can only be updated & accessible in that loop.

Solution 6:[6]

let has a block-scope - that means that the variable will only exist within its surrounding pair of curly brackets. So, in this situation when we try to print the value of let after the loop, it will return undefined.

while (true) {
    let test = getValue();
    console.log(test); // => some value...
    ....
}
console.log(test); // => undefined

In this situation, test is already defined before the while loop (curly brackets), so it will be preserved outside of the while loop:

let test;
while (true) {
    test = getValue();
    console.log(test); // => some value...
    ....
}
console.log(test); // => still some value...

Note that even if you use let inside the while loop as well as outside, it will treat it the same:

let test = 5;
while (true) {
    let test = 4;
    console.log(test); // => 4
    ....
}
console.log(test); // => 4, not 5

So don't go trying to define your own special block-scoped variable independent of the main one ?

I will not give a runnable code example otherwise you will end up in an infinite loop ?

Solution 7:[7]

let is block-scoped which means it will only exist within a {...} block.

so, if you want to access the let value outside of the loop, you have to use this,

let data;

while (true) {
  data = getData()
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Lucas Rosenberger
Solution 3 T.J. Crowder
Solution 4 Prateek
Solution 5
Solution 6 Ethan
Solution 7 ahuemmer