If statement and scope


#1

Hey for some reason I can’t post in the JavaScript section of the forum, so here goes…

If I have a “let” being declared/assigned in an if statement that evaluates to true… what is the scope of that let?

Is the if statement considered a block of its own or since its true does it release the variable to the parent block that the if statement is contained within?


#2

It is, I believe in the scope of the if statement code block in which it is declared. But we need to test this assumption to be sure.

Update

Bingo!

 > if (true) {let x = 1;}
<- undefined
 > x
 ! ReferenceError: x is not defined

Yes, as is any block structure. We can write anonymous blocks and use let inside them to prevent the variables from leaking into the local scope.

 > {
      let x = 0;
   }
<- undefined
 > x
 ! ReferenceError: x is not defined

The code block is valid, useful and has its own scope.

Aside

When working in the Web Console (JavaScript Console) we can copy and enter multiline code not contained in a function by containing it in a block. Drop the whole thing at the command prompt and Enter. Zing, the whole thing runs like a kitten.

 > {
     var c = 0;
     {
       let a = b = c;
       do {
         a++; b += a; c += b;
         console.log(a, b, c);
       } while (a < 10);
     }
     console.log(c);
   }
   1 1 1
   2 3 4
   3 6 10
   4 10 20
   5 15 35
   6 21 56
   7 28 84
   8 36 120
   9 45 165
   10 55 220
   220
<- undefined
 > c
<- 220
 > 

Note that by using var to declare c it is locally scoped, meaning we can still access it when the code is finished.


In more tecnical terms, given we pasted this into the global object, window, var is moot. c is globally scoped. witness…

 > window.c
<- 220

Because this is an anonymous block in context of window we won’t need to declare it, only define it.

c = 0;

The deciding factor at this point, and remember we are sitting at the command prompt in the JS console. is whether or not we want access to the variables inside the block when all is said and done. If not for any valid reason other than debugging, then definitely shut them off from the outside world and use let inside the block.

let c = 0

Now, garbage collection will clean up the memory all three variables occupied and we will no longer have access to any of them from the command prompt. If your modules test out this way once they’re debugged then your code is managing memory well.


Advanced topic related to this subject. Immediately Invoked Function Expresssion, or IIFE.

 > (() => {
     var c = 0;
     {
       let a = b = c;
       do {
         a++; b += a; c += b;
         console.log(a, b, c);
       } while (a < 10);
     }
     console.log(c);
   })()
   1 1 1
   2 3 4
   3 6 10
   4 10 20
   5 15 35
   6 21 56
   7 28 84
   8 36 120
   9 45 165
   10 55 220
   220
 > 

Notice that when we passed just a code block to the command prompt we got the same result? That’s because the command prompt IS an IIFE. We give it a code body. That’s our command.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.