Declaring variable within for statement 💬

Consider the following code:

function reverseString(str) {
  for (var reversedStr = "", i = str.length - 1; i >= 0; i--) {
    reversedStr += str[i];
  }
  return reversedStr;
}

Why is the variable var reversedStr = "" is declared inside, and how to declare variables like that and how many variables we can declare like that and why the counter i is not declared?
Thanks in advance!

@bedward You have not declared the i variable. There is nowhere on the page that says let, var or const i = . It would also be better to create reversedStr outside of the for loop. Happy Coding! :smile:

Hey @landon.h.lloyd,

Statements such as var one, two, three are indeed allowed in javascript.

So in this case, i is definitely declared. :slight_smile:

1 Like

Probably not intentionally though, I suspect that , was intended as a ;.

Hi @bedward,

I’m unclear if by “inside” you mean inside the for loop or inside the reverseString function.

You can declare as many variables as you want on a single line like so:

var one, two, three, four, five;

or

var one = 1, two = 2, three = 3;

and so on.

Whether you choose to do this is a matter of style and readability. My personal preference is to avoid this style opting instead to split things into multiple lines for readability. But again, this is a preference.

Declaring a variable inside of the for loop does make a difference - but only if you use let instead of var. In this case, putting var reversedStr inside of the for loop doesn’t change the program.

It does make a difference if you use let instead of var, however.

Using let limits where the variable can be used (this is called the “scope” of that variable).

If you have this:

for (let i = 0; i < 10; i++) {
  console.log(i);
}

console.log(i);

You will get an error saying that i is not declared when it is encountered inside of the second console.log(i).

But if you change it to this:

let i;
for (i = 0 ; i < 10; i++) {
   console.log(i);
}
console.log(i);

Everything will be fine and the second console.log(i) will print out the expected value.

Hope that helps!

1 Like

Possible but then there would be 4 clauses inside the for loop :stuck_out_tongue:

I totally missed the i== at the end when I looked at it. That’s what I get for not being careful. :expressionless:

That’s a large part of why I almost never write multiple declarations on a single line even inside for loops. Our eyes are used to certain patterns and it’s waaay too easy to misread things like this :slight_smile:

1 Like

Thank you very much for explaining with details, so appreciated! A little additional question, can i do something like that : var one = 1, let two = 2, var three = 3; or if i started with var or let i have to stick to it?

btw nice picture :joy:

@jefejeff Oh that is good to know. Thank you!

It is not allowed.

I didn’t know off the top of my head either so here is how I tested it:

(Feel free to give it a shot yourself too :slight_smile: This is a copy/paste from my terminal)

jeff:~/code$ node
Welcome to Node.js v14.15.0.
Type ".help" for more information.
> var one = 1, let two = 2;
var one = 1, let two = 2;
                 ^^^

Uncaught SyntaxError: Unexpected identifier

And now I’m curious - Is there a reason you’ve found for doing this in practice or were you just wondering if it worked?

Just in case I’d like to add some unsolicited advice :slight_smile:

Use let unless you have a good reason for var.
let was introduced into the language in order to fix common issues with var.

var creates global variables every time. This means you can access them anywhere.
let creates what are called “block scope” variables, which means they are only accessible within the “block” they are created. In javascript a block is (usually) the nearest surrounding curly braces ({ this is a block }). But there are sensible exceptions, like in this case with the for (...) { } section. Here, the stuff in the parenthesis is also included in the for loop’s block.

You can read waaay more info than you’re likely to want about it on mozilla’s javascript documentation page on let.

1 Like

Thanks haha! I think I took a picture at a sushi restaurant a long time ago and kept it as my avatar in a few places lol.

Was just wondering, Thank you for the reply and the advice!