sleepless_in_seattle
sleepless_in_seattle

Reputation: 2214

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();
    ....
}

Upvotes: 9

Views: 7627

Answers (8)

klugjo
klugjo

Reputation: 20885

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

You should use the first form if you do not plan on accessing the variable test outside of the while loop.

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

You should use the second form if you need to access test from outside the while loop or to reuse it across iterations.

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

Please also note that it is better to use const if you are not planning on reassigning another value to test.

Upvotes: 11

Jeevan Prasad
Jeevan Prasad

Reputation: 1

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()
}

Upvotes: 0

Ethan
Ethan

Reputation: 3808

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 😂

Upvotes: 0

Tushar Walzade
Tushar Walzade

Reputation: 3819

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.

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074666

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.

Upvotes: 3

Faly
Faly

Reputation: 13356

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

Upvotes: 4

Prateek
Prateek

Reputation: 342

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

Upvotes: 2

Lucas Rosenberger
Lucas Rosenberger

Reputation: 255

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.

Upvotes: 3

Related Questions