3

for (index1 = 1; index1 < 8; index1++) {
  var op = '#';
  for (index2 = index1; index2 - 1; index2--) {
    op = op + '#';

  }
  console.log(op);
}

In the above code and in the statement "var op = '#';", Is variable 'op' defined and initialized every time in 'for' iteration or defined once and initialized every time with the specific value..

xGeo
  • 2,149
  • 2
  • 18
  • 39
Gunacelan M
  • 179
  • 2
  • 10

6 Answers6

5

Your code is the same as the below snippet, because of variable hoisting. This means that op is declared once, but initialized to # every iteration of the outer loop.

var op;
for(index1 = 1 ;index1<8;index1++)
{
    op = '#'; 
    for (index2 = index1; index2-1; index2--) 
    {
        op = op + '#';

    }
    console.log(op);
}
XCS
  • 27,244
  • 26
  • 101
  • 151
1

Here is your snippet which will log # in incrementing value for each iteration in your outer loop:

for (index1 = 1; index1 < 8; index1++) {
  var op = '#';
  for (index2 = index1; index2 - 1; index2--) {
    op = op + '#';
  }
  console.log(op);
}

To explain why is that so, first, let's dissect your loops:

for (index1 = 1; index1 < 8; index1++) {
  var op = '#';
. . . 

In this part, you created a variable op with a value #. For 7 iterations, it will be re-instantiated to the default value #.

But why is that the variable increments for each iteration? Let's move on to the inner loop:

for (index2 = index1; index2 - 1; index2--) {
    op = op + '#';
}

For each iteration inside this inner loop, you are appending an additional # to the default value of op.

Anyway, let's see how many iterations the inner loop is making for each iteration of the outer loop:

var x = 0;

for (index1 = 1; index1 < 8; index1++) {
  //var op = '#';
  for (index2 = index1; index2 - 1; index2--) {
    //op = op + '#';
    x++;
  }
  console.log(x);
  x = 0;
  //console.log(op);
}

As you can see, it's making 0, 1, 2, 3, 4, 5, 6 iterations for each outer loop iteration. Meaning, on the first iteration, it doesn't append a #, adds 1 # on the second, and so on and so forth. So it justifies the results on your code.

#
##
###
####
#####
######
#######

Side note: Noticed something weird? Honestly, I find this loop very weird. But if I'm correct, it is because 0 is falsy and thus it wont execute the iteration, which is the second parameter in the loop. Please add a comment if my guess is right.

Anyway, I guess I have to post that as a question. I'll update this answer once I get responses from my question.

EDIT: Sorry, there were only 7 iterations for the console.log(x). As for the weirdness of the loop condition, my guess is correct. index2 - 1 can also be written as index2 - 1 != 0 or more accurately !!(index2 - 1). !! converts anything into Boolean. Any falsey value will become false: 0, null, ''(empty string), undefined, NaN, and false.

As for your question (darn, i almost forgot coz of the weird loop), lets go back to the code:

for (index1 = 1; index1 < 8; index1++) {
  var op = '#';
  for (index2 = index1; index2 - 1; index2--) {
    op = op + '#';
  }
  //console.log(op);
}
//let's see if the variable resides outside the outer loop

console.log(op);

TADA! It still logs #######. This is because of variable hoisting as what @Cristy said.

xGeo
  • 2,149
  • 2
  • 18
  • 39
  • Outer for-loop is iterated from 1 to 7 and there comes 7 logs.Look again at the output.. – Gunacelan M Oct 07 '17 at 15:40
  • oh weird.. i thought they were nine. i tried the code in the chrome debugger (*coz I lost briefly lost my internet connection*) and it logged 8 times – xGeo Oct 07 '17 at 15:46
0

The scope of the variable will be function scope, because you are using var.

So the compiler / interpreter will move this variable to the beginning of the function through a process called hoisting.

If you want block scope, please use let instead of var.

In the case of your code let and var do not seem to make a difference in the output.

gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
0

The var variable is created at the top of the enclosing function, or since you're in the global scope, it's created as the script initializes in the global scope.

Within the outer loop, it's basically just an assignment of #, overwriting any previous value.

Within the inner loop, it appends another # to the value on each iteration.

If you used a let variable instead of var, then the variable would be scoped to the block of the outer loop, and inaccessible outside that block, though still accessible inside the inner loop.

llama
  • 2,535
  • 12
  • 11
0

"Is variable 'op' defined and initialized every time in 'for' iteration or defined once and initialized every time with the specific value?"

The op variable creates again on every parent loop iteration (for (index1 = 1; index1 < 8; index1++) {...}), but for every child for loop, it exists and you just override its value from child loop.

P.S.
  • 15,970
  • 14
  • 62
  • 86
0

"Is variable 'op' defined and initialized every time"

No.

"var" is a directive for the parser, and not a command executed at run-time. It makes no difference whether value is declared to be var inside the loop, outside the loop, or both.

see this post

I think it would be better for readability if the variable was declared outside of the loop.

This is a scope question

Shawn
  • 3,031
  • 4
  • 26
  • 53