Difference between var and let in a for loop

Tell us what’s happening:
Hi! Why the for loop works with var but not with let?

Your code so far

function reverseString(str) {
for (var reversed = "", i = str.length - 1; i >= 0; i--){ //if I use let instead of var it says "ReferenceError: reversed is not defined"
  reversed += str[i];

return reversed;


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0.

Challenge: Reverse a String

Link to the challenge:

var has a wider scope and is valid outside of the loop body while let is only in scope for the loop.

In this case, I would use

let reversed = "";
for (let i = str.length - 1; i >= 0; i--) {

Why do I need to make my “reversed” variable wider?
Is it because the “return reversed;” line would not work otherwise?

You used ‘reversed’ outside of the loop, so it needs to be defined outside of the loop.